Skip to content

Commit 33a1eb9

Browse files
authored
migrate to vitest (#2314)
* vitest * rename jest to vi * fix test * remove .skip tests * make last test works
1 parent f3df301 commit 33a1eb9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+917
-2101
lines changed

.npmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
save-exact=true
1+
save-exact=true

jest.config.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"prettier": "prettier --cache --write --list-different .",
3232
"release": "pnpm build && pnpm changeset publish",
3333
"release:canary": "npm run release -- canary",
34-
"test": "jest"
34+
"test": "vitest ."
3535
},
3636
"dependencies": {
3737
"@sentry/node": "7.41.0",
@@ -52,15 +52,13 @@
5252
"@types/body-parser": "1.19.2",
5353
"@types/cors": "2.8.13",
5454
"@types/express": "4.17.17",
55-
"@types/jest": "27.5.2",
5655
"@types/node": "18.15.5",
5756
"@types/yargs": "17.0.3",
5857
"@zeit/ncc": "0.22.3",
5958
"bob-the-bundler": "1.7.3",
6059
"eslint": "8.35.0",
6160
"graphql": "16.6.0",
6261
"graphql-config": "4.5.0",
63-
"jest": "27.5.1",
6462
"jsesc": "3.0.2",
6563
"lint-staged": "13.1.2",
6664
"lodash": "4.17.21",
@@ -69,8 +67,8 @@
6967
"rimraf": "4.3.1",
7068
"smee-client": "1.2.3",
7169
"strip-ansi": "6.0.1",
72-
"ts-jest": "27.0.5",
73-
"typescript": "4.2.4"
70+
"typescript": "4.2.4",
71+
"vitest": "0.29.7"
7472
},
7573
"sideEffects": false,
7674
"lint-staged": {

packages/action/__tests__/run.test.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,29 @@ import { fileLoader } from '../src/files';
66
import { getAssociatedPullRequest } from '../src/git';
77
import { run } from '../src/run';
88

9-
jest.mock('../src/checks');
10-
jest.mock('../src/git');
11-
jest.mock('../src/files');
9+
vi.mock('../src/checks');
10+
vi.mock('../src/git');
11+
vi.mock('../src/files');
1212

13-
const mockUpdateCheckRun = updateCheckRun as jest.MockedFunction<typeof updateCheckRun>;
14-
const mockFileLoader = fileLoader as jest.MockedFunction<typeof fileLoader>;
15-
const mockGetAssociatedPullRequest = getAssociatedPullRequest as jest.MockedFunction<
13+
const mockUpdateCheckRun = updateCheckRun as vi.MockedFunction<typeof updateCheckRun>;
14+
const mockFileLoader = fileLoader as vi.MockedFunction<typeof fileLoader>;
15+
const mockGetAssociatedPullRequest = getAssociatedPullRequest as vi.MockedFunction<
1616
typeof getAssociatedPullRequest
1717
>;
1818

1919
describe('Inspector Action', () => {
20-
const mockLoadFile = jest.fn();
20+
const mockLoadFile = vi.fn();
2121

2222
beforeEach(() => {
23-
jest.clearAllMocks();
23+
vi.clearAllMocks();
2424

2525
// Mock error/warning/info/debug
26-
jest.spyOn(core, 'error').mockImplementation(jest.fn());
27-
jest.spyOn(core, 'warning').mockImplementation(jest.fn());
28-
jest.spyOn(core, 'info').mockImplementation(jest.fn());
29-
jest.spyOn(core, 'debug').mockImplementation(jest.fn());
26+
vi.spyOn(core, 'error').mockImplementation(vi.fn());
27+
vi.spyOn(core, 'warning').mockImplementation(vi.fn());
28+
vi.spyOn(core, 'info').mockImplementation(vi.fn());
29+
vi.spyOn(core, 'debug').mockImplementation(vi.fn());
3030

31-
jest.spyOn(core, 'getInput').mockImplementation((name: string, _options) => {
31+
vi.spyOn(core, 'getInput').mockImplementation((name: string, _options) => {
3232
switch (name) {
3333
case 'github-token':
3434
return 'MOCK_GITHUB_TOKEN';
@@ -39,16 +39,16 @@ describe('Inspector Action', () => {
3939
}
4040
});
4141

42-
jest.spyOn(github, 'getOctokit').mockReturnValue({
42+
vi.spyOn(github, 'getOctokit').mockReturnValue({
4343
checks: {
44-
create: jest.fn().mockResolvedValue({
44+
create: vi.fn().mockResolvedValue({
4545
data: {
4646
id: '2',
4747
},
4848
}),
4949
},
5050
});
51-
jest.spyOn(github.context, 'repo', 'get').mockImplementation(() => {
51+
vi.spyOn(github.context, 'repo', 'get').mockImplementation(() => {
5252
return {
5353
owner: 'some-owner',
5454
repo: 'graphql-inspector',
@@ -69,7 +69,7 @@ describe('Inspector Action', () => {
6969

7070
describe('rules', () => {
7171
it('should accept a rules list with 1 built in rule', async () => {
72-
jest.spyOn(core, 'getInput').mockImplementation((name: string, _options) => {
72+
vi.spyOn(core, 'getInput').mockImplementation((name: string, _options) => {
7373
switch (name) {
7474
case 'github-token':
7575
return 'MOCK_GITHUB_TOKEN';
@@ -129,7 +129,7 @@ describe('Inspector Action', () => {
129129
});
130130

131131
it('should accept a rules list with 1 custom rule', async () => {
132-
jest.spyOn(core, 'getInput').mockImplementation((name: string, _options) => {
132+
vi.spyOn(core, 'getInput').mockImplementation((name: string, _options) => {
133133
switch (name) {
134134
case 'github-token':
135135
return 'MOCK_GITHUB_TOKEN';
@@ -190,7 +190,7 @@ describe('Inspector Action', () => {
190190
});
191191

192192
it('should accept a rules list with a built-in and a custom rule', async () => {
193-
jest.spyOn(core, 'getInput').mockImplementation((name: string, _options) => {
193+
vi.spyOn(core, 'getInput').mockImplementation((name: string, _options) => {
194194
switch (name) {
195195
case 'github-token':
196196
return 'MOCK_GITHUB_TOKEN';

packages/action/jest.config.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/ci/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"prebuild": "pnpm clean",
4343
"prepack": "bob prepack",
4444
"prepublishOnly": "pnpm build",
45-
"test": "jest"
45+
"test": "vitest ."
4646
},
4747
"peerDependencies": {
4848
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"

packages/cli/jest.config.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"prebuild": "pnpm clean",
4242
"prepack": "bob prepack",
4343
"prepublishOnly": "pnpm build",
44-
"test": "jest"
44+
"test": "vitest ."
4545
},
4646
"peerDependencies": {
4747
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"

packages/commands/commands/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isAbsolute, resolve } from 'path';
22
import yargs, { CommandModule as Command } from 'yargs';
3+
import { hideBin } from 'yargs/helpers';
34
import { InspectorConfig } from '@graphql-inspector/config';
45
import { Loaders } from '@graphql-inspector/loaders';
56

@@ -78,5 +79,5 @@ export function parseGlobalArgs(args: GlobalArgs) {
7879
}
7980

8081
export async function mockCommand(mod: Command, cmd: string) {
81-
return yargs.command(mod).parseAsync(cmd);
82+
return yargs(hideBin(process.argv)).command(mod).parseAsync(cmd);
8283
}

packages/commands/diff/__tests__/diff-command.spec.ts renamed to packages/commands/diff/__tests__/diff-command.test.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,30 @@ import { buildSchema } from 'graphql';
33
import yargs from 'yargs';
44
import { mockCommand } from '@graphql-inspector/commands';
55
import { mockLogger, unmockLogger } from '@graphql-inspector/logger';
6-
import '@graphql-inspector/testing';
76
import createCommand from '../src';
87

8+
/*
9+
* yargs copies value of `process.exit` in `lib/platform-shims/esm.mjs` file,
10+
* but after our vitest mocking of `process.exit` doesn't work and exits Node.js process with
11+
* Error: process.exit called with "1"
12+
*/
13+
vi.mock('yargs', async () => {
14+
const yargsPath = require.resolve('yargs').replace('index.cjs', '');
15+
const { YargsFactory } = await vi.importActual(yargsPath + 'build/lib/yargs-factory.js');
16+
const { default: esmPlatformShim } = await vi.importActual(
17+
yargsPath + 'lib/platform-shims/esm.mjs',
18+
);
19+
return {
20+
default: YargsFactory({
21+
...esmPlatformShim,
22+
process: {
23+
...esmPlatformShim.process,
24+
exit: () => null,
25+
},
26+
}),
27+
};
28+
});
29+
930
const oldSchema = buildSchema(/* GraphQL */ `
1031
type Post {
1132
id: ID
@@ -36,33 +57,21 @@ const diff = createCommand({
3657
loaders: [],
3758
},
3859
loaders: {
39-
async loadSchema(pointer) {
40-
if (pointer.includes('old')) {
41-
return oldSchema;
42-
}
43-
44-
return newSchema;
45-
},
46-
async loadDocuments() {
47-
throw new Error('Not implemented');
48-
},
60+
loadSchema: pointer => (pointer.includes('old') ? oldSchema : newSchema),
4961
},
5062
});
5163

5264
describe('diff', () => {
53-
let spyReporter: jest.SpyInstance;
54-
let spyProcessExit: jest.SpyInstance;
55-
let spyProcessCwd: jest.SpyInstance;
65+
let spyReporter: vi.SpyInstance;
66+
let spyProcessExit: vi.SpyInstance;
67+
let spyProcessCwd: vi.SpyInstance;
5668

5769
beforeEach(() => {
5870
yargs();
59-
spyProcessExit = jest.spyOn(process, 'exit');
60-
spyProcessExit.mockImplementation();
61-
62-
spyProcessCwd = jest.spyOn(process, 'cwd').mockImplementation(() => __dirname);
63-
64-
spyReporter = jest.fn();
65-
mockLogger(spyReporter as any);
71+
spyProcessExit = vi.spyOn(process, 'exit').mockImplementation(() => null);
72+
spyProcessCwd = vi.spyOn(process, 'cwd').mockImplementation(() => __dirname);
73+
spyReporter = vi.fn();
74+
mockLogger(spyReporter);
6675
});
6776

6877
afterEach(() => {

packages/commands/introspect/__tests__/introspect-command.spec.ts renamed to packages/commands/introspect/__tests__/introspect-command.test.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,14 @@ const introspect = createCommand({
3939
});
4040

4141
describe('introspect', () => {
42-
let spyReporter: jest.SpyInstance;
43-
let spyProcessCwd: jest.SpyInstance;
42+
let spyReporter: vi.SpyInstance;
43+
let spyProcessCwd: vi.SpyInstance;
4444

4545
beforeEach(() => {
4646
yargs();
47-
48-
spyProcessCwd = jest.spyOn(process, 'cwd').mockImplementation(() => __dirname);
49-
50-
spyReporter = jest.fn();
51-
mockLogger(spyReporter as any);
47+
spyProcessCwd = vi.spyOn(process, 'cwd').mockImplementation(() => __dirname);
48+
spyReporter = vi.fn();
49+
mockLogger(spyReporter);
5250
});
5351

5452
afterEach(() => {

packages/commands/validate/__tests__/validate-command.ts renamed to packages/commands/validate/__tests__/validate-command.test.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { buildSchema, parse } from 'graphql';
33
import yargs from 'yargs';
44
import { mockCommand } from '@graphql-inspector/commands';
55
import { mockLogger, unmockLogger } from '@graphql-inspector/logger';
6-
import '@graphql-inspector/testing';
76
import createCommand from '../src';
87

98
const schema = buildSchema(/* GraphQL */ `
@@ -67,18 +66,15 @@ const validate = createCommand({
6766
});
6867

6968
describe('validate', () => {
70-
let spyReporter: jest.SpyInstance;
71-
let spyProcessExit: jest.SpyInstance;
72-
let spyProcessCwd: jest.SpyInstance;
69+
let spyReporter: vi.SpyInstance;
70+
let spyProcessExit: vi.SpyInstance;
71+
let spyProcessCwd: vi.SpyInstance;
7372

7473
beforeEach(() => {
75-
spyProcessExit = jest.spyOn(process, 'exit');
76-
spyProcessExit.mockImplementation();
77-
78-
spyProcessCwd = jest.spyOn(process, 'cwd').mockImplementation(() => __dirname);
79-
80-
spyReporter = jest.fn();
81-
mockLogger(spyReporter as any);
74+
spyProcessExit = vi.spyOn(process, 'exit').mockImplementation(() => null);
75+
spyProcessCwd = vi.spyOn(process, 'cwd').mockImplementation(() => __dirname);
76+
spyReporter = vi.fn();
77+
mockLogger(spyReporter);
8278
});
8379

8480
afterEach(() => {

packages/core/.npmignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
__tests__/
22
src/
3-
jest.config.js
43
tsconfig.json
54
tsconfig.test.json
65
*.zip

packages/core/__tests__/coverage/coverage.ts renamed to packages/core/__tests__/coverage/coverage.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { buildSchema, getIntrospectionQuery, parse, print, Source } from 'graphql';
2-
import { coverage } from '../../src/index';
2+
import { coverage } from '../../src';
33

44
describe('coverage', () => {
55
const schema = buildSchema(/* GraphQL */ `

packages/core/__tests__/diff/argument.ts renamed to packages/core/__tests__/diff/argument.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { buildSchema } from 'graphql';
2-
import { CriticalityLevel } from '../../src/diff/changes/change';
3-
import { diff } from '../../src/index';
2+
import { CriticalityLevel, diff } from '../../src';
43
import { findFirstChangeByPath } from '../../utils/testing';
54

65
describe('argument', () => {

packages/core/__tests__/diff/directive.ts renamed to packages/core/__tests__/diff/directive.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { buildSchema } from 'graphql';
2-
import { CriticalityLevel } from '../../src/diff/changes/change';
3-
import { diff } from '../../src/index';
2+
import { CriticalityLevel, diff } from '../../src';
43
import { findFirstChangeByPath } from '../../utils/testing';
54

65
describe('directive', () => {

packages/core/__tests__/diff/enum.ts renamed to packages/core/__tests__/diff/enum.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { buildSchema } from 'graphql';
2-
import { CriticalityLevel } from '../../src/diff/changes/change';
3-
import { diff, DiffRule } from '../../src/index';
2+
import { CriticalityLevel, diff, DiffRule } from '../../src';
43
import { findFirstChangeByPath } from '../../utils/testing';
54

65
describe('enum', () => {

packages/core/__tests__/diff/input.ts renamed to packages/core/__tests__/diff/input.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { buildSchema } from 'graphql';
2-
import { CriticalityLevel, diff, DiffRule } from '../../src/index';
2+
import { CriticalityLevel, diff, DiffRule } from '../../src';
33
import { findFirstChangeByPath } from '../../utils/testing';
44

55
describe('input', () => {

packages/core/__tests__/diff/interface.ts renamed to packages/core/__tests__/diff/interface.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { buildSchema } from 'graphql';
2-
import { CriticalityLevel, diff } from '../../src/index';
2+
import { CriticalityLevel, diff } from '../../src';
33
import { findChangesByPath, findFirstChangeByPath } from '../../utils/testing';
44

55
describe('interface', () => {

packages/core/__tests__/diff/object.ts renamed to packages/core/__tests__/diff/object.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { buildSchema } from 'graphql';
2-
import { CriticalityLevel, diff, DiffRule } from '../../src/index';
2+
import { CriticalityLevel, diff, DiffRule } from '../../src';
33
import { findChangesByPath, findFirstChangeByPath } from '../../utils/testing';
44

55
describe('object', () => {

packages/core/__tests__/diff/rules/consider-usage.ts renamed to packages/core/__tests__/diff/rules/consider-usage.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { buildSchema } from 'graphql';
2-
import { considerUsage } from '../../../src/diff/rules/consider-usage';
3-
import { CriticalityLevel, diff } from '../../../src/index';
2+
import { CriticalityLevel, diff } from '../../../src';
3+
import { considerUsage } from '../../../src/diff/rules';
44
import { findFirstChangeByPath } from '../../../utils/testing';
55

66
describe('considerUsage rule', () => {

0 commit comments

Comments
 (0)