Skip to content

Commit

Permalink
GREEN
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Jul 31, 2024
1 parent a93e888 commit 59e722d
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 68 deletions.
33 changes: 15 additions & 18 deletions packages/vest/src/__tests__/isolate.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
Expand Down Expand Up @@ -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',
),
);
});
});
Expand Down
50 changes: 25 additions & 25 deletions packages/vest/src/core/test/__tests__/key.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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[][] = [];
Expand All @@ -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]);
Expand Down Expand Up @@ -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();
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { deferThrow } from 'vest-utils';
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';

import { TTestSuite } from '../../../testUtils/TVestMock';
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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([
Expand All @@ -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', () => {
Expand Down
24 changes: 16 additions & 8 deletions packages/vest/src/isolates/__tests__/each.test.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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();
});
});
});
Expand Down
10 changes: 9 additions & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
}),
],
});

0 comments on commit 59e722d

Please sign in to comment.