Skip to content

Commit

Permalink
Swap out jest for vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
IanVS committed Apr 21, 2023
1 parent 21840dc commit 4d01cd6
Show file tree
Hide file tree
Showing 59 changed files with 752 additions and 2,278 deletions.
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public/
.gitignore
.prettierignore
prettier.config.js
jest.config.js
vitest.config.js
yarn.lock
tsconfig.json
CODE_OF_CONDUCT.md
Expand Down
4 changes: 2 additions & 2 deletions docs/DEBUG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ yarn run compile && node --inspect-brk ./node_modules/.bin/prettier --config exa
You can set a `debugger` anywhere in the code and then use following command:

```shell
node --inspect-brk ./node_modules/.bin/jest -i
node --inspect-brk ./node_modules/.bin/vitest --run
```

Or, to debug a single unit test file

```shell
node --inspect-brk ./node_modules/.bin/jest -i <name-or-relative-path-of-the-file-file>
node --inspect-brk ./node_modules/.bin/vitest --run <name-or-relative-path-of-the-file-file>
```

### How to run prettier in any codebase using the plugin ?
Expand Down
14 changes: 0 additions & 14 deletions jest.config.js

This file was deleted.

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"compile": "tsc",
"preexample": "yarn run compile",
"example": "prettier --config ./examples/.prettierrc --plugin lib/src/index.js",
"test": "jest -i",
"test": "vitest --run",
"type-check": "tsc --noEmit",
"prepublishOnly": "npm run compile && npm run test"
},
Expand Down Expand Up @@ -55,15 +55,16 @@
"lodash.isequal": "^4.5.0"
},
"devDependencies": {
"@types/jest": "^29.2.0",
"@types/babel__generator": "^7.6.4",
"@types/babel__traverse": "^7.18.3",
"@types/lodash.clone": "4.5.7",
"@types/lodash.isequal": "4.5.6",
"@types/node": "^18.15.13",
"@types/prettier": "^2.7.2",
"@vue/compiler-sfc": "3.2.47",
"jest": "^29.5.0",
"prettier": "2.8.7",
"ts-jest": "^29.1.0",
"typescript": "5.0.4"
"typescript": "5.0.4",
"vitest": "^0.30.1"
},
"peerDependencies": {
"@vue/compiler-sfc": ">=3.0.0",
Expand Down
37 changes: 17 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface PrettierOptionSchema {
description: string;
}

const options: Record<
export const options: Record<
Exclude<keyof PrettierOptions, keyof PrettierRequiredOptions>,
PrettierOptionSchema
> = {
Expand Down Expand Up @@ -83,24 +83,21 @@ const options: Record<
},
};

module.exports = {
parsers: {
babel: {
...babelParsers.babel,
preprocess: defaultPreprocessor,
},
flow: {
...flowParsers.flow,
preprocess: defaultPreprocessor,
},
typescript: {
...typescriptParsers.typescript,
preprocess: defaultPreprocessor,
},
vue: {
...htmlParsers.vue,
preprocess: vuePreprocessor,
},
export const parsers = {
babel: {
...babelParsers.babel,
preprocess: defaultPreprocessor,
},
flow: {
...flowParsers.flow,
preprocess: defaultPreprocessor,
},
typescript: {
...typescriptParsers.typescript,
preprocess: defaultPreprocessor,
},
vue: {
...htmlParsers.vue,
preprocess: vuePreprocessor,
},
options,
};
2 changes: 2 additions & 0 deletions src/utils/__tests__/adjust-comments-on-sorted-nodes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { expect, test } from 'vitest';

import type { ImportOrLine } from '../../types';
import { adjustCommentsOnSortedNodes } from '../adjust-comments-on-sorted-nodes';
import { getImportNodes } from '../get-import-nodes';
Expand Down
2 changes: 2 additions & 0 deletions src/utils/__tests__/explode-type-and-value-specifiers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { expect, test } from 'vitest';

import { explodeTypeAndValueSpecifiers } from '../explode-type-and-value-specifiers';
import { getCodeFromAst } from '../get-code-from-ast';
import { getImportNodes } from '../get-import-nodes';
Expand Down
1 change: 1 addition & 0 deletions src/utils/__tests__/get-all-comments-from-nodes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ParserOptions } from '@babel/parser';
import { CommentBlock, CommentLine, ImportDeclaration } from '@babel/types';
import { expect, test } from 'vitest';

import { getAllCommentsFromNodes } from '../get-all-comments-from-nodes';
import { getImportNodes } from '../get-import-nodes';
Expand Down
2 changes: 2 additions & 0 deletions src/utils/__tests__/get-chunk-type-of-node.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { expect, test } from 'vitest';

import { chunkTypeOther, chunkTypeUnsortable } from '../../constants';
import { getChunkTypeOfNode } from '../get-chunk-type-of-node';
import { getImportNodes } from '../get-import-nodes';
Expand Down
5 changes: 3 additions & 2 deletions src/utils/__tests__/get-code-from-ast.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { format } from 'prettier';
import { expect, test } from 'vitest';

import { getCodeFromAst } from '../get-code-from-ast';
import { getImportNodes } from '../get-import-nodes';
import { getSortedNodes } from '../get-sorted-nodes';

it('sorts imports correctly', () => {
test('sorts imports correctly', () => {
const code = `// first comment
// second comment
import z from 'z';
Expand Down Expand Up @@ -43,7 +44,7 @@ import z from "z";
);
});

it('merges duplicate imports correctly', () => {
test('merges duplicate imports correctly', () => {
const code = `// first comment
// second comment
import z from 'z';
Expand Down
2 changes: 2 additions & 0 deletions src/utils/__tests__/get-experimental-parser-plugins.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { expect, test } from 'vitest';

import { getExperimentalParserPlugins } from '../get-experimental-parser-plugins';

test('it should return empty list', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/__tests__/get-import-flavor-of-node.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { expect, test } from 'vitest';

import { getImportFlavorOfNode } from '../get-import-flavor-of-node';
import { getImportNodes } from '../get-import-nodes';

it('should correctly classify a bunch of import expressions', () => {
test('should correctly classify a bunch of import expressions', () => {
expect(
getImportNodes(
`
Expand Down
2 changes: 2 additions & 0 deletions src/utils/__tests__/get-import-nodes-matched-group.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { expect, test } from 'vitest';

import { getImportNodes } from '../get-import-nodes';
import { getImportNodesMatchedGroup } from '../get-import-nodes-matched-group';

Expand Down
2 changes: 2 additions & 0 deletions src/utils/__tests__/get-sorted-import-specifiers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { expect, test } from 'vitest';

import { getImportNodes } from '../get-import-nodes';
import { getSortedImportSpecifiers } from '../get-sorted-import-specifiers';
import { getSortedNodesModulesNames } from '../get-sorted-nodes-modules-names';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ImportDeclaration } from '@babel/types';
import { expect, test } from 'vitest';

import { getImportNodes } from '../get-import-nodes';
import { getSortedNodesByImportOrder } from '../get-sorted-nodes-by-import-order';
Expand Down
1 change: 1 addition & 0 deletions src/utils/__tests__/get-sorted-nodes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ImportDeclaration } from '@babel/types';
import { expect, test } from 'vitest';

import { getImportNodes } from '../get-import-nodes';
import { getSortedNodes } from '../get-sorted-nodes';
Expand Down
23 changes: 12 additions & 11 deletions src/utils/__tests__/merge-nodes-with-matching-flavors.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { format } from 'prettier';
import { expect, test } from 'vitest';

import { getCodeFromAst } from '../get-code-from-ast';
import { getImportNodes } from '../get-import-nodes';
Expand All @@ -15,7 +16,7 @@ const defaultOptions = {
importOrderSortSpecifiers: true,
};

it('should merge duplicate imports within a given chunk', () => {
test('should merge duplicate imports within a given chunk', () => {
const code = `
import type { A } from 'a';
import { Junk } from 'junk-group-1'
Expand Down Expand Up @@ -89,7 +90,7 @@ import { Junk2 } from "junk-group-2";
`);
});

it('should merge type imports into regular imports', () => {
test('should merge type imports into regular imports', () => {
const code = `
// Preserves 'import type'
import type { A1 } from 'a';
Expand Down Expand Up @@ -132,7 +133,7 @@ import { D1, type D2 } from "d";
`);
});

it('should combine type import and default import', () => {
test('should combine type import and default import', () => {
const code = `
import type {MyType} from './source';
import defaultValue from './source';
Expand All @@ -158,7 +159,7 @@ import defaultValue from './source';
`);
});

it('should not combine type import and namespace import', () => {
test('should not combine type import and namespace import', () => {
const code = `
import type {MyType} from './source';
import * as Namespace from './source';
Expand All @@ -185,7 +186,7 @@ import * as Namespace from "./source";
`);
});

it('should support aliased named imports', () => {
test('should support aliased named imports', () => {
const code = `
import type {MyType} from './source';
import {value as alias} from './source';
Expand All @@ -211,7 +212,7 @@ import {value as alias} from './source';
`);
});

it('should combine multiple imports from the same source', () => {
test('should combine multiple imports from the same source', () => {
const code = `
import type {MyType, SecondType} from './source';
import {value, SecondValue} from './source';
Expand All @@ -237,7 +238,7 @@ import {value, SecondValue} from './source';
`);
});

it('should combine multiple groups of imports', () => {
test('should combine multiple groups of imports', () => {
const code = `
import type {MyType} from './source';
import type {OtherType} from './other';
Expand Down Expand Up @@ -266,7 +267,7 @@ import { value, type MyType } from "./source";
`);
});

it('should combine multiple imports statements from the same source', () => {
test('should combine multiple imports statements from the same source', () => {
const code = `
import type {MyType} from './source';
import type {SecondType} from './source';
Expand Down Expand Up @@ -294,7 +295,7 @@ import {SecondValue} from './source';
`);
});

it('should not impact imports from different sources', () => {
test('should not impact imports from different sources', () => {
const code = `
import type {MyType} from './source';
import type {OtherType} from './other';
Expand Down Expand Up @@ -324,7 +325,7 @@ import { thirdValue } from "./third";
`);
});

it("doesn't merge duplicate imports if option disabled", () => {
test("doesn't merge duplicate imports if option disabled", () => {
const code = `
import type { A } from 'a';
import { Junk } from 'junk-group-1'
Expand Down Expand Up @@ -401,7 +402,7 @@ import { Junk2 } from "junk-group-2";
`);
});

it('should not combine default type imports', () => {
test('should not combine default type imports', () => {
const code = `
import { ComponentProps, useEffect } from "react";
import type React from "react";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { parse as babelParser } from '@babel/parser';
import { format } from 'prettier';
import { expect, test } from 'vitest';

import { getAllCommentsFromNodes } from '../get-all-comments-from-nodes';
import { getImportNodes } from '../get-import-nodes';
Expand Down
6 changes: 4 additions & 2 deletions test-setup/raw-serializer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

import { expect } from 'vitest';

const RAW = Symbol.for('raw');

module.exports = {
expect.addSnapshotSerializer({
print(val) {
return val[RAW];
},
Expand All @@ -13,4 +15,4 @@ module.exports = {
typeof val[RAW] === 'string'
);
},
};
});
26 changes: 12 additions & 14 deletions test-setup/run_spec.js → test-setup/run_spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
'use strict';

const fs = require('fs');
const extname = require('path').extname;
const prettier = require('prettier');
import fs from 'fs';
import { extname, join, resolve } from 'path';
import prettier from 'prettier';
import { expect, test } from 'vitest';

function run_spec(dirname, parsers, options) {
import * as plugin from '../src';

export function run_spec(dirname, parsers, options) {
options = Object.assign(
{
plugins: ['./src'],
plugins: [plugin],
tabWidth: 4,
},
options,
Expand All @@ -24,7 +27,7 @@ function run_spec(dirname, parsers, options) {
extname(filename) !== '.snap' &&
fs.lstatSync(path).isFile() &&
filename[0] !== '.' &&
filename !== 'ppsi.spec.js'
filename !== 'ppsi.spec.ts'
) {
const source = read(path).replace(/\r\n/g, '\n');

Expand All @@ -33,13 +36,9 @@ function run_spec(dirname, parsers, options) {
});
const output = prettyprint(source, path, mergedOptions);
test(`${filename} - ${mergedOptions.parser}-verify`, () => {
try {
expect(
raw(source + '~'.repeat(80) + '\n' + output),
).toMatchSnapshot(filename);
} catch (e) {
console.error(e, path);
}
expect(
raw(source + '~'.repeat(80) + '\n' + output),
).toMatchSnapshot(filename);
});

parsers.slice(1).forEach((parserName) => {
Expand All @@ -58,7 +57,6 @@ function run_spec(dirname, parsers, options) {
}
});
}
global.run_spec = run_spec;

function prettyprint(src, filename, options) {
return prettier.format(
Expand Down

0 comments on commit 4d01cd6

Please sign in to comment.