Skip to content

Commit

Permalink
test: move mocks example to core tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Apr 30, 2024
1 parent 6c535d6 commit b6ce2d4
Show file tree
Hide file tree
Showing 65 changed files with 424 additions and 763 deletions.
857 changes: 288 additions & 569 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

@@ -1,4 +1,5 @@
import { ref } from 'vue'
import { vi } from 'vitest'

export const useJwt = vi.fn(() => ({
payload: ref({
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,4 +1,5 @@
import actualCreate from 'zustand'
import { vi } from 'vitest'

// when creating a store, we get its initial state, create a reset function and add it in the set
const create = vi.fn((createState) => {
Expand Down
7 changes: 6 additions & 1 deletion test/core/package.json
Expand Up @@ -18,11 +18,16 @@
"@vitest/test-dep2": "file:./deps/dep2",
"@vitest/utils": "workspace:*",
"@vitest/web-worker": "workspace:^",
"axios": "^0.26.1",
"debug": "^4.3.4",
"strip-ansi": "^7.1.0",
"sweetalert2": "^11.6.16",
"tinyspy": "^1.0.2",
"url": "^0.11.0",
"useJwt": "link:@vueuse/integrations/useJwt",
"vitest": "workspace:*",
"vitest-environment-custom": "file:./vitest-environment-custom"
"vitest-environment-custom": "file:./vitest-environment-custom",
"vue": "^3.4.26",
"zustand": "^4.1.1"
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions test/core/src/mocks/external/external.mjs
@@ -0,0 +1,3 @@
import { vi } from 'vitest'

vi.doMock('axios')
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,14 +1,15 @@
import type * as exampleModule from '../src/example.js'
import log from '../src/log.js'
import { A } from '../src/moduleA.js'
import { methodSymbol, moduleWithSymbol } from '../src/moduleWithSymbol.js'
import { expect, test, vi } from 'vitest'
import type * as exampleModule from '../../src/mocks/example'
import log from '../../src/mocks/log'
import { A } from '../../src/mocks/moduleA'
import { methodSymbol, moduleWithSymbol } from '../../src/mocks/moduleWithSymbol'

vi.mock('../src/log')
vi.mock('../src/moduleA')
vi.mock('../src/moduleWithSymbol')
vi.mock('../../src/mocks/log')
vi.mock('../../src/mocks/moduleA')
vi.mock('../../src/mocks/moduleWithSymbol')

test('all mocked are valid', async () => {
const example = await vi.importMock<typeof exampleModule>('../src/example')
const example = await vi.importMock<typeof exampleModule>('../../src/mocks/example')

// creates a new mocked function with no formal arguments.
expect(example.square.name).toEqual('square')
Expand Down
@@ -1,4 +1,5 @@
import axios from 'axios'
import { expect, test, vi } from 'vitest'

vi.mock('axios')

Expand Down
@@ -1,4 +1,5 @@
import axios from 'axios'
import { expect, test, vi } from 'vitest'

test('mocked axios', async () => {
const { default: ax } = await vi.importMock<any>('axios')
Expand Down
28 changes: 28 additions & 0 deletions test/core/test/mocking/circular-mocks.spec.ts
@@ -0,0 +1,28 @@
import { expect, test, vi } from 'vitest'
// @ts-expect-error not typed
import { main, mainB } from '../../src/mocks/main.js'
import x from '../../src/mocks/export-default-circle-index.js'

vi.mock('../../src/mocks/A', async () => ({
...(await vi.importActual<any>('../../src/mocks/A')),
funcA: () => 'mockedA',
}))

vi.mock('../../src/mocks/B', async () => ({
...(await vi.importActual<any>('../../src/mocks/B')),
funcB: () => 'mockedB',
}))

vi.mock('../../src/mocks/export-default-circle-b')

test('can import actual inside mock factory', () => {
expect(main()).toBe('mockedA')
})

test('can import in top level and inside mock factory', () => {
expect(mainB()).toBe('mockedB')
})

test('can mock a circular dependency', () => {
expect(x()).toBe(undefined)
})
@@ -1,5 +1,6 @@
// @ts-expect-error not typed aliased import
import getState from 'custom-lib'
import { expect, it, vi } from 'vitest'

vi.mock('custom-lib')

Expand Down
13 changes: 13 additions & 0 deletions test/core/test/mocking/cyclic-import-actual.spec.ts
@@ -0,0 +1,13 @@
import { expect, test, vi } from 'vitest'

import '../../src/mocks/cyclic-deps/module-1'

vi.mock('../../src/mocks/cyclic-deps/module-2', async () => {
await vi.importActual('../../src/mocks/cyclic-deps/module-2')

return { default: () => {} }
})

test('some test', () => {
expect(1 + 1).toBe(2)
})
@@ -1,8 +1,8 @@
import { expect, test, vi } from 'vitest'

import '../src/cyclic-deps/module-1.js'
import '../../src/mocks/cyclic-deps/module-1'

vi.mock('../src/cyclic-deps/module-2', async (importOriginal) => {
vi.mock('../../src/mocks/cyclic-deps/module-2', async (importOriginal) => {
await importOriginal()

return { default: () => {} }
Expand Down
17 changes: 17 additions & 0 deletions test/core/test/mocking/destructured.test.ts
@@ -0,0 +1,17 @@
import { expect, test, vi } from 'vitest'
// @ts-expect-error mocked module
import * as squaredModule from '../../../mocks/src/squared'
// @ts-expect-error mocked module
import { squared } from '../../../mocks/src/squared'
import { foo } from '../../../mocks/src/set-foo.js'

vi.mock('any')

test('spyOn entire module', () => {
vi.spyOn(squaredModule, 'squared')
expect(squared).not.toHaveBeenCalled()
})

test('foo should be 1', () => {
expect(foo).toBe(1)
})
@@ -1,8 +1,10 @@
vi.mock('../src/default', () => {
import { expect, test, vi } from 'vitest'

vi.mock('../../src/mocks/default', () => {
throw new Error('some error')
})

test('when using top level variable, gives helpful message', async () => {
await expect(() => import('../src/default.js').then(m => m.default)).rejects
await expect(() => import('../../src/mocks/default').then(m => m.default)).rejects
.toThrowErrorMatchingInlineSnapshot(`[Error: [vitest] There was an error when mocking a module. If you are using "vi.mock" factory, make sure there are no top level variables inside, since this call is hoisted to top of the file. Read more: https://vitest.dev/api/vi.html#vi-mock]`)
})
@@ -1,9 +1,10 @@
import '../src/external/external.mjs'
import '../../src/mocks/external/external.mjs'
import { expect, test, vi } from 'vitest'
import axios from 'axios'
import defaultFunc from '../src/external/default-function.cjs'
// @ts-expect-error mocked module
import defaultFunc from '../../src/mocks/external/default-function.cjs'

vi.mock('../src/external/default-function.cjs')
vi.mock('../../src/mocks/external/default-function.cjs')

test('axios is mocked', () => {
expect(vi.isMockFunction(axios.get)).toBe(true)
Expand Down
@@ -1,11 +1,12 @@
import axios from 'axios'
import * as example from '../src/example.js'
import * as moduleA from '../src/moduleA.js'
import * as moduleB from '../src/moduleB.js'
import logger from '../src/log.js'
import { describe, expect, it, test, vi } from 'vitest'
import * as example from '../../src/mocks/example'
import * as moduleA from '../../src/mocks/moduleA'
import * as moduleB from '../../src/mocks/moduleB'
import logger from '../../src/mocks/log'

vi
.mock('../src/example', () => ({
.mock('../../src/mocks/example', () => ({
mocked: true,
then: 'a then export',
ok: undefined,
Expand All @@ -14,19 +15,19 @@ vi
}))

// doesn't think comments are mocks
// vi.mock('../src/example', () => ({
// vi.mock('../../src/mocks/example', () => ({
// mocked: false,
// }))

vi.mock('../src/moduleA', async () => {
const actual = await vi.importActual<any>('../src/moduleA')
vi.mock('../../src/mocks/moduleA', async () => {
const actual = await vi.importActual<any>('../../src/mocks/moduleA')
return {
B: 'B',
...actual,
}
})

vi.mock('../src/moduleB', async (importOriginal) => {
vi.mock('../../src/mocks/moduleB', async (importOriginal) => {
const actual = await importOriginal<any>()
return {
...actual,
Expand All @@ -43,9 +44,9 @@ vi.mock('axios', () => {
}
})

vi.mock('../src/log.ts', async () => {
vi.mock('../../src/mocks/log.ts', async () => {
// can import the same module inside and does not go into an infinite loop
const log = await import('../src/log.js')
const log = await import('../../src/mocks/log')
return {
default: {
...log.default,
Expand All @@ -54,20 +55,20 @@ vi.mock('../src/log.ts', async () => {
}
})

vi.mock('../src/default.ts', () => null)
vi.mock('../../src/mocks/default.ts', () => null)

describe('mocking with factory', () => {
test('missing exports on mock', () => {
expect(() => example.default).toThrowError('[vitest] No "default" export is defined on the "../src/example" mock')
expect(() => example.boolean).toThrowError('[vitest] No "boolean" export is defined on the "../src/example" mock')
expect(() => example.object).toThrowError('[vitest] No "object" export is defined on the "../src/example" mock')
expect(() => example.array).toThrowError('[vitest] No "array" export is defined on the "../src/example" mock')
expect(() => example.someClasses).toThrowError('[vitest] No "someClasses" export is defined on the "../src/example" mock')
expect(() => example.default).toThrowError('[vitest] No "default" export is defined on the "../../src/mocks/example" mock')
expect(() => example.boolean).toThrowError('[vitest] No "boolean" export is defined on the "../../src/mocks/example" mock')
expect(() => example.object).toThrowError('[vitest] No "object" export is defined on the "../../src/mocks/example" mock')
expect(() => example.array).toThrowError('[vitest] No "array" export is defined on the "../../src/mocks/example" mock')
expect(() => example.someClasses).toThrowError('[vitest] No "someClasses" export is defined on the "../../src/mocks/example" mock')
})

it('non-object return on factory gives error', async () => {
await expect(() => import('../src/default.js').then(m => m.default)).rejects
.toThrowError('[vitest] vi.mock("../src/default.ts", factory?: () => unknown) is not returning an object. Did you mean to return an object with a "default" key?')
await expect(() => import('../../src/mocks/default.js').then(m => m.default)).rejects
.toThrowError('[vitest] vi.mock("../../src/mocks/default.ts", factory?: () => unknown) is not returning an object. Did you mean to return an object with a "default" key?')
})

test('defined exports on mock', async () => {
Expand Down
@@ -1,5 +1,6 @@
// @ts-expect-error mocked module
import { mocked } from 'extension.js'
import { expect, test, vi } from 'vitest'

vi.mock('extension.js')

Expand Down
@@ -1,5 +1,5 @@
import { expect, test, vi } from 'vitest'
import { asyncSquare as importedAsyncSquare, square as importedSquare } from '../src/example.js'
import { asyncSquare as importedAsyncSquare, square as importedSquare } from '../../src/mocks/example'

const mocks = vi.hoisted(() => {
return {
Expand All @@ -13,7 +13,7 @@ const { asyncSquare } = await vi.hoisted(async () => {
}
})

vi.mock('../src/example.ts', () => {
vi.mock('../../src/mocks/example', () => {
return {
square: mocks.square,
asyncSquare,
Expand Down
@@ -1,4 +1,5 @@
import { createStore } from '../src/integration.js'
import { expect, test, vi } from 'vitest'
import { createStore } from '../../src/mocks/integration'

vi.mock('@vueuse/integrations/useJwt')

Expand Down
@@ -1,10 +1,12 @@
// @vitest-environment jsdom

import sweetalert from 'sweetalert2'
import * as modDefaultCjs from '../src/external/default-cjs.cjs'
import { expect, test, vi } from 'vitest'
// @ts-expect-error mocked module
import * as modDefaultCjs from '../../src/mocks/external/default-cjs.cjs'

vi.mock('sweetalert2')
vi.mock('../src/external/default-cjs.cjs')
vi.mock('../../src/mocks/external/default-cjs.cjs')

test('default is mocked', () => {
expect(vi.isMockFunction(modDefaultCjs.default.fn)).toBe(true)
Expand Down
@@ -1,6 +1,7 @@
import { retryDynamicImport } from '../src/retry-dynamic-import.js'
import { describe, expect, it, vi } from 'vitest'
import { retryDynamicImport } from '../../src/mocks/retry-dynamic-import.js'

vi.mock('../src/dynamic-module', () => {
vi.mock('../../src/mocks/dynamic-module', () => {
return { foo: 'bar' }
})

Expand All @@ -9,7 +10,7 @@ describe('retry-dynamic-import', () => {
expect(await retryDynamicImport()).toEqual({ foo: 'bar' })
})
it('should throw when retry over 3 times', async () => {
vi.doMock('../src/dynamic-module', () => {
vi.doMock('../../src/mocks/dynamic-module', () => {
throw new Error('foobar')
})
await expect(retryDynamicImport()).rejects.toThrowError(new Error('import dynamic module failed.'))
Expand Down
@@ -1,5 +1,6 @@
import zustand from 'zustand'
import { magic } from '../src/zustand-magic.js'
import { describe, expect, test, vi } from 'vitest'
import { magic } from '../../src/mocks/zustand-magic'

vi.mock('zustand')

Expand Down
8 changes: 8 additions & 0 deletions test/core/test/mocking/spaced.spec.ts
@@ -0,0 +1,8 @@
import { expect, test, vi } from 'vitest'
import { thisIsOk } from '../../src/mocks/has space in path'

vi.mock('../../src/mocks/has space in path', () => ({ thisIsOk: true }))

test('modules with spaces in name is mocked', () => {
expect(thisIsOk).toBe(true)
})
@@ -1,4 +1,5 @@
import type * as tinyspyModule from 'tinyspy'
import { expect, test, vi } from 'vitest'

test('tinyspy is not mocked with __mocks__, but automatically mocked', async () => {
const tinyspy = await vi.importMock<typeof tinyspyModule>('tinyspy')
Expand Down
Expand Up @@ -6,6 +6,7 @@ import * as vscodeFactory from 'vscode-factory'

// @ts-expect-error virtual module
import * as virtual from 'virtual-module'
import { expect, it, vi } from 'vitest'

vi.mock('vscode-mocks')
vi.mock('vscode-factory', () => {
Expand Down
32 changes: 0 additions & 32 deletions test/mocks/package.json

This file was deleted.

3 changes: 0 additions & 3 deletions test/mocks/projects/custom-lib/index.js

This file was deleted.

3 changes: 0 additions & 3 deletions test/mocks/projects/custom-lib/package.json

This file was deleted.

1 change: 0 additions & 1 deletion test/mocks/src/external/external.mjs

This file was deleted.

0 comments on commit b6ce2d4

Please sign in to comment.