test: mock semver/functions/satisfies

This commit is contained in:
Pooya Parsa 2019-12-17 10:59:12 +01:00
parent 51b3163c2a
commit 311ff27ba6

View File

@ -1,6 +1,7 @@
import path from 'path'
import consola from 'consola'
import fsExtra from 'fs-extra'
import semver from 'semver'
import { r, waitFor } from '@nuxt/utils'
import { BundleBuilder } from '@nuxt/webpack'
@ -8,7 +9,7 @@ import Builder from '../src/builder'
import { createNuxt } from './__utils__'
jest.mock('fs-extra')
// jest.mock('semver')
jest.mock('semver/functions/satisfies')
jest.mock('hash-sum', () => src => `hash(${src})`)
jest.mock('@nuxt/utils')
jest.mock('../src/ignore')
@ -235,9 +236,9 @@ describe('builder: builder build', () => {
}
}
const builder = new Builder(nuxt, BundleBuilder)
// semver.satisfies
// .mockReturnValueOnce(true)
// .mockReturnValueOnce(true)
semver.satisfies
.mockReturnValueOnce(true)
.mockReturnValueOnce(true)
nuxt.resolver.requireModule
.mockReturnValueOnce({ version: 'alpha' })
.mockReturnValueOnce({ version: 'beta' })
@ -247,9 +248,9 @@ describe('builder: builder build', () => {
expect(nuxt.resolver.requireModule).toBeCalledTimes(2)
expect(nuxt.resolver.requireModule).nthCalledWith(1, 'join(vue, package.json)')
expect(nuxt.resolver.requireModule).nthCalledWith(2, 'join(nuxt, package.json)')
// expect(semver.satisfies).toBeCalledTimes(2)
// expect(semver.satisfies).nthCalledWith(1, 'alpha', 'latest')
// expect(semver.satisfies).nthCalledWith(2, 'beta', 'edge')
expect(semver.satisfies).toBeCalledTimes(2)
expect(semver.satisfies).nthCalledWith(1, 'alpha', 'latest')
expect(semver.satisfies).nthCalledWith(2, 'beta', 'edge')
})
test('should warn and throw error if dependencies is not installed', () => {
@ -261,7 +262,8 @@ describe('builder: builder build', () => {
}
}
const builder = new Builder(nuxt, BundleBuilder)
// semver.satisfies.mockReturnValueOnce(false)
semver.satisfies
.mockReturnValueOnce(false)
nuxt.resolver.requireModule
.mockReturnValueOnce({ version: 'alpha' })
.mockReturnValueOnce(undefined)
@ -274,7 +276,7 @@ describe('builder: builder build', () => {
expect(consola.warn).toBeCalledTimes(2)
expect(consola.warn).nthCalledWith(1, 'vue@latest is recommended but vue@alpha is installed!')
expect(consola.warn).nthCalledWith(2, 'nuxt@edge is required but not installed!')
// expect(semver.satisfies).toBeCalledTimes(1)
// expect(semver.satisfies).nthCalledWith(1, 'alpha', 'latest')
expect(semver.satisfies).toBeCalledTimes(1)
expect(semver.satisfies).nthCalledWith(1, 'alpha', 'latest')
})
})