From 59e722d892a0f7d33ea707416faadc4b231b1342 Mon Sep 17 00:00:00 2001 From: Evyatar Date: Wed, 31 Jul 2024 15:32:24 +0300 Subject: [PATCH] GREEN --- packages/vest/src/__tests__/isolate.test.ts | 33 ++++++------ .../vest/src/core/test/__tests__/key.test.ts | 50 +++++++++---------- .../merging_of_previous_test_runs.test.ts | 35 +++++++------ .../vest/src/isolates/__tests__/each.test.ts | 24 ++++++--- vitest.config.ts | 10 +++- 5 files changed, 84 insertions(+), 68 deletions(-) diff --git a/packages/vest/src/__tests__/isolate.test.ts b/packages/vest/src/__tests__/isolate.test.ts index 735825bd5..37887843e 100644 --- a/packages/vest/src/__tests__/isolate.test.ts +++ b/packages/vest/src/__tests__/isolate.test.ts @@ -1,30 +1,25 @@ -import { CB } from 'vest-utils'; +import { CB, deferThrow } from 'vest-utils'; import { Isolate } from 'vestjs-runtime'; import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest'; -import { TVestMock } from '../testUtils/TVestMock'; -import { TDummyTest } from '../testUtils/testDummy'; +import { dummyTest } from 'testDummy'; +import * as vest from 'vest'; -describe.skip('isolate', () => { - let vest: TVestMock; +describe('isolate', () => { let firstRun = true; - import('IsolateTest'); - import('IsolateEach'); - let dummyTest: TDummyTest; - - const _to = setTimeout; beforeEach(() => { firstRun = true; - // @ts-ignore - global.setTimeout = (cb: any) => cb(); - import('IsolateTest'); - import('IsolateEach'); - dummyTest = require('../testUtils/testDummy').dummyTest; + vi.mock('vest-utils', async () => { + const vu = await vi.importActual('vest-utils'); + return { + ...vu, + deferThrow: vi.fn(), + }; + }); }); afterEach(() => { - global.setTimeout = _to; vi.resetModules(); vi.resetAllMocks(); }); @@ -251,8 +246,10 @@ describe.skip('isolate', () => { suite(); suite(); - expect(suite).toThrow( - 'Vest Critical Error: Tests called in different order than previous run', + expect(deferThrow).toHaveBeenCalledWith( + expect.stringContaining( + 'Vest Critical Error: Tests called in different order than previous run', + ), ); }); }); diff --git a/packages/vest/src/core/test/__tests__/key.test.ts b/packages/vest/src/core/test/__tests__/key.test.ts index 309dd24a5..3706d4e8f 100644 --- a/packages/vest/src/core/test/__tests__/key.test.ts +++ b/packages/vest/src/core/test/__tests__/key.test.ts @@ -1,11 +1,24 @@ -import { TIsolateTest } from 'IsolateTest'; -import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'; - -import { TVestMock } from '../../../testUtils/TVestMock'; +import { deferThrow } from 'vest-utils'; +import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest'; +import { TIsolateTest } from 'IsolateTest'; import * as vest from 'vest'; describe('key', () => { + beforeEach(() => { + vi.mock('vest-utils', async () => { + const vu = await vi.importActual('vest-utils'); + return { + ...vu, + deferThrow: vi.fn(), + }; + }); + }); + + afterEach(() => { + vi.resetAllMocks(); + }); + describe('When key is provided', () => { describe('When tests change their order between runs', () => { it('Should retain test results', () => { @@ -76,14 +89,8 @@ describe('key', () => { }); describe('When tests without a key reorder get added above a test with a key', () => { - const _to = setTimeout; - beforeEach(() => { - global.setTimeout = vi.fn(cb => cb()); - }); afterEach(() => { - global.setTimeout = _to; vi.resetModules(); - vi.resetAllMocks(); }); it('Should retain keyd tests', () => { const calls: TIsolateTest[][] = []; @@ -108,6 +115,8 @@ describe('key', () => { const res1 = suite(); const res2 = suite(); + expect(deferThrow).toHaveBeenCalled(); + expect(calls[0][0]).toBe(calls[1][0]); expect(calls[0][1]).toBe(calls[1][1]); expect(calls[0][2]).toBe(calls[1][2]); @@ -199,27 +208,18 @@ describe('key', () => { }); describe('When the same key is encountered twice', () => { - const _to = setTimeout; - let toMock = vi.fn(); - beforeEach(() => { - toMock = vi.fn(cb => CB()); - global.setTimeout = toMock; - }); - - afterEach(() => { - global.setTimeout = _to; - vi.resetAllMocks(); - vi.resetModules(); - }); - - it('Should throw a deferred error', () => { + it('Should throw a deferred error', async () => { const suite = vest.create(() => { vest.test('field1', () => false, 'key_1'); vest.test('field2', () => false, 'key_1'); }); - expect(suite).toThrow( + + suite(); + expect(deferThrow).toHaveBeenCalledWith( `Encountered the same key "key_1" twice. This may lead to inconsistent or overriding of results.`, ); + + vi.resetAllMocks(); }); }); }); diff --git a/packages/vest/src/core/test/__tests__/merging_of_previous_test_runs.test.ts b/packages/vest/src/core/test/__tests__/merging_of_previous_test_runs.test.ts index 5d877d903..8e9fd8b2c 100644 --- a/packages/vest/src/core/test/__tests__/merging_of_previous_test_runs.test.ts +++ b/packages/vest/src/core/test/__tests__/merging_of_previous_test_runs.test.ts @@ -1,3 +1,4 @@ +import { deferThrow } from 'vest-utils'; import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'; import { TTestSuite } from '../../../testUtils/TVestMock'; @@ -15,6 +16,18 @@ describe('Merging of previous test runs', () => { beforeEach(() => { counter = 0; testContainer = []; + + vi.mock('vest-utils', async () => { + const vu = await vi.importActual('vest-utils'); + return { + ...vu, + deferThrow: vi.fn(), + }; + }); + }); + + afterEach(() => { + vi.resetAllMocks(); }); describe('When test skipped in subsequent run', () => { it('Should merge its result from previous runs', () => { @@ -77,18 +90,6 @@ describe('Merging of previous test runs', () => { }); describe('When tests are passed in a different order between runs', () => { - const _to = setTimeout; - let stoMock: vi.Mock; - beforeEach(() => { - stoMock = vi.fn(cb => cb()); - global.setTimeout = stoMock; - }); - - afterEach(() => { - global.setTimeout = _to; - vi.resetAllMocks(); - }); - it('Should defer-throw an error', () => { suite = vest.create(() => { testContainer.push([ @@ -100,12 +101,14 @@ describe('Merging of previous test runs', () => { }); suite(); - expect(stoMock).not.toHaveBeenCalled(); + expect(deferThrow).not.toHaveBeenCalled(); - expect(suite).toThrow( - 'Vest Critical Error: Tests called in different order than previous run.', + suite(); + expect(deferThrow).toHaveBeenCalledWith( + expect.stringContaining( + 'Vest Critical Error: Tests called in different order than previous run.', + ), ); - expect(stoMock).not.toHaveBeenCalled(); }); describe('When test is omitted in subsequent run', () => { diff --git a/packages/vest/src/isolates/__tests__/each.test.ts b/packages/vest/src/isolates/__tests__/each.test.ts index 074f1b866..e571f203b 100644 --- a/packages/vest/src/isolates/__tests__/each.test.ts +++ b/packages/vest/src/isolates/__tests__/each.test.ts @@ -1,11 +1,22 @@ -import { TDeferThrow } from 'vest-utils/src/deferThrow'; +import { deferThrow } from 'vest-utils'; import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'; -import { TVestMock } from '../../testUtils/TVestMock'; - import * as vest from 'vest'; describe('each', () => { + beforeEach(() => { + vi.mock('vest-utils', async () => { + const vu = await vi.importActual('vest-utils'); + return { + ...vu, + deferThrow: vi.fn(), + }; + }); + }); + + afterEach(() => { + vi.resetAllMocks(); + }); describe('When callback is not a function', () => { it('should throw', () => { const control = vi.fn(); @@ -64,11 +75,8 @@ describe('each', () => { }); suite(); - const _to = setTimeout; - // @ts-expect-error - global.setTimeout = cb => cb(); - expect(() => suite()).toThrow(); - global.setTimeout = _to; + suite(); + expect(deferThrow).toHaveBeenCalled(); }); }); }); diff --git a/vitest.config.ts b/vitest.config.ts index 736555f2b..4ad47d879 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -11,7 +11,15 @@ export default defineConfig({ plugins: [ tsconfigPaths({ loose: true, - projects: ["packages/vest-utils","packages/context","packages/vestjs-runtime","packages/vast","packages/n4s","packages/vest","packages/anyone"], + projects: [ + 'packages/vest-utils', + 'packages/context', + 'packages/vestjs-runtime', + 'packages/vast', + 'packages/n4s', + 'packages/vest', + 'packages/anyone', + ], }), ], });