Skip to content

Commit

Permalink
test: migrate to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Jan 27, 2024
1 parent e0e6b5b commit f626df7
Show file tree
Hide file tree
Showing 46 changed files with 1,154 additions and 941 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ lib/
coverage/
node_modules/
fixtures/
/auto-imports.d.ts
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = {
'import/no-extraneous-dependencies': [
'error',
{
devDependencies: ['**/*.test.js', '**/*.test.ts'],
devDependencies: ['**/*.test.js', '**/*.test.ts', 'vitest.config.ts'],
},
],
},
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ coverage/
node_modules/
fixtures/
CHANGELOG.md
/auto-imports.d.ts
1 change: 1 addition & 0 deletions @commitlint/config-angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"type": "module",
"version": "18.6.0",
"description": "Shareable commitlint config enforcing the angular commit convention",
"main": "index.js",
"files": [
"index.js"
],
Expand Down
1 change: 1 addition & 0 deletions @commitlint/config-conventional/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"type": "module",
"version": "18.6.0",
"description": "Shareable commitlint config enforcing conventional commits",
"main": "index.js",
"files": [
"index.js"
],
Expand Down
1 change: 1 addition & 0 deletions @commitlint/config-lerna-scopes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"type": "module",
"version": "18.4.4",
"description": "Shareable commitlint config enforcing lerna package and workspace names as scopes",
"main": "index.js",
"files": [
"index.js"
],
Expand Down
116 changes: 58 additions & 58 deletions @commitlint/config-validator/src/__snapshots__/validate.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,135 +1,135 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`validation should fail for defaultIgnoresNotBoolean 1`] = `
"Commitlint configuration in defaultIgnoresNotBoolean.js is invalid:
exports[`validation should fail for > defaultIgnoresNotBoolean 1`] = `
[Error: Commitlint configuration in defaultIgnoresNotBoolean.js is invalid:
- Property "defaultIgnores" has the wrong type - must be boolean.
"
]
`;

exports[`validation should fail for extendsAsObject 1`] = `
"Commitlint configuration in extendsAsObject.js is invalid:
exports[`validation should fail for > extendsAsObject 1`] = `
[Error: Commitlint configuration in extendsAsObject.js is invalid:
- Property "extends" has the wrong type - must be array.
- Property "extends" has the wrong type - must be string.
- "/extends" must match exactly one schema in oneOf. Value: {"test":1}.
"
]
`;

exports[`validation should fail for extendsWithFunction 1`] = `
"Commitlint configuration in extendsWithFunction.js is invalid:
exports[`validation should fail for > extendsWithFunction 1`] = `
[Error: Commitlint configuration in extendsWithFunction.js is invalid:
- Property "extends/0" has the wrong type - must be string.
- Property "extends" has the wrong type - must be string.
- "/extends" must match exactly one schema in oneOf. Value: [null].
"
]
`;

exports[`validation should fail for formatterAsObject 1`] = `
"Commitlint configuration in formatterAsObject.js is invalid:
exports[`validation should fail for > formatterAsObject 1`] = `
[Error: Commitlint configuration in formatterAsObject.js is invalid:
- Property "formatter" has the wrong type - must be string.
"
]
`;

exports[`validation should fail for helpUrlAsArray 1`] = `
"Commitlint configuration in helpUrlAsArray.js is invalid:
exports[`validation should fail for > helpUrlAsArray 1`] = `
[Error: Commitlint configuration in helpUrlAsArray.js is invalid:
- Property "helpUrl" has the wrong type - must be string.
"
]
`;

exports[`validation should fail for helpUrlNotString 1`] = `
"Commitlint configuration in helpUrlNotString.js is invalid:
exports[`validation should fail for > helpUrlNotString 1`] = `
[Error: Commitlint configuration in helpUrlNotString.js is invalid:
- Property "helpUrl" has the wrong type - must be string.
"
]
`;

exports[`validation should fail for ignoresFunction 1`] = `
"Commitlint configuration in ignoresFunction.js is invalid:
exports[`validation should fail for > ignoresFunction 1`] = `
[Error: Commitlint configuration in ignoresFunction.js is invalid:
- Property "ignores" has the wrong type - must be array.
"
]
`;

exports[`validation should fail for ignoresNotFunction 1`] = `
"Commitlint configuration in ignoresNotFunction.js is invalid:
exports[`validation should fail for > ignoresNotFunction 1`] = `
[Error: Commitlint configuration in ignoresNotFunction.js is invalid:
- "/ignores/0" should be a function. Value: 1.
"
]
`;

exports[`validation should fail for parserPreset 1`] = `
"Commitlint configuration in parserPreset.js is invalid:
exports[`validation should fail for > parserPreset 1`] = `
[Error: Commitlint configuration in parserPreset.js is invalid:
- Property "parserPreset" has the wrong type - must be string.
- Property "parserPreset" has the wrong type - must be object.
- "/parserPreset" should be a function. Value: [].
- "/parserPreset" must match exactly one schema in oneOf. Value: [].
"
]
`;

exports[`validation should fail for pluginsNotArray 1`] = `
"Commitlint configuration in pluginsNotArray.js is invalid:
exports[`validation should fail for > pluginsNotArray 1`] = `
[Error: Commitlint configuration in pluginsNotArray.js is invalid:
- Property "plugins" has the wrong type - must be array.
"
]
`;

exports[`validation should fail for rules1 1`] = `
"Commitlint configuration in rules1.js is invalid:
exports[`validation should fail for > rules1 1`] = `
[Error: Commitlint configuration in rules1.js is invalid:
- "/rules/a/0" must be equal to one of the allowed values. Value: 3.
- "/rules/a" should be a function. Value: [3].
- "/rules/a" must match exactly one schema in oneOf. Value: [3].
"
]
`;

exports[`validation should fail for rules2 1`] = `
"Commitlint configuration in rules2.js is invalid:
exports[`validation should fail for > rules2 1`] = `
[Error: Commitlint configuration in rules2.js is invalid:
- "/rules/b" must NOT have more than 3 items. Value: [1,"test",2,2].
- "/rules/b" should be a function. Value: [1,"test",2,2].
- "/rules/b" must match exactly one schema in oneOf. Value: [1,"test",2,2].
"
]
`;

exports[`validation should fail for rules3 1`] = `
"Commitlint configuration in rules3.js is invalid:
exports[`validation should fail for > rules3 1`] = `
[Error: Commitlint configuration in rules3.js is invalid:
- "/rules/c" must NOT have fewer than 1 items. Value: [].
- "/rules/c" should be a function. Value: [].
- "/rules/c" must match exactly one schema in oneOf. Value: [].
"
]
`;

exports[`validation should fail for rules4 1`] = `
"Commitlint configuration in rules4.js is invalid:
exports[`validation should fail for > rules4 1`] = `
[Error: Commitlint configuration in rules4.js is invalid:
- Property "rules/d/0" has the wrong type - must be number.
- "/rules/d/0" must be equal to one of the allowed values. Value: [].
- "/rules/d" should be a function. Value: [[],[],[]].
- "/rules/d" must match exactly one schema in oneOf. Value: [[],[],[]].
"
]
`;

exports[`validation should fail for rules5 1`] = `
"Commitlint configuration in rules5.js is invalid:
exports[`validation should fail for > rules5 1`] = `
[Error: Commitlint configuration in rules5.js is invalid:
- Property "rules/e" has the wrong type - must be array.
- "/rules/e" should be a function. Value: {}.
- "/rules/e" must match exactly one schema in oneOf. Value: {}.
"
]
`;

exports[`validation should fail for rulesAsArray 1`] = `
"Commitlint configuration in rulesAsArray.js is invalid:
exports[`validation should fail for > rulesAsArray 1`] = `
[Error: Commitlint configuration in rulesAsArray.js is invalid:
- Property "rules" has the wrong type - must be object.
"
]
`;

exports[`validation should fail for whenConfigIsNotObject 1`] = `
"Commitlint configuration in whenConfigIsNotObject.js is invalid:
exports[`validation should fail for > whenConfigIsNotObject 1`] = `
[Error: Commitlint configuration in whenConfigIsNotObject.js is invalid:
- Config has the wrong type - must be object.
"
]
`;

exports[`validation should fail for whenConfigIsNotObject2 1`] = `
"Commitlint configuration in whenConfigIsNotObject2.js is invalid:
exports[`validation should fail for > whenConfigIsNotObject2 1`] = `
[Error: Commitlint configuration in whenConfigIsNotObject2.js is invalid:
- Config has the wrong type - must be object.
"
]
`;

exports[`validation should fail for withPluginsAsObject 1`] = `
"Commitlint configuration in withPluginsAsObject.js is invalid:
exports[`validation should fail for > withPluginsAsObject 1`] = `
[Error: Commitlint configuration in withPluginsAsObject.js is invalid:
- Property "plugins/0" has the wrong type - must be string.
- "/plugins/0" must have required property 'rules'. Value: {}.
- "/plugins/0" must match a schema in anyOf. Value: {}.
"
]
`;
10 changes: 4 additions & 6 deletions @commitlint/cz-commitlint/src/Process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import {QualifiedRules, RuleConfigSeverity, UserPromptConfig} from '@commitlint/
import {Answers, DistinctQuestion} from 'inquirer';
import isFunction from 'lodash.isfunction';

import {jest} from '@jest/globals';
import process from './Process.js';

import process from './Process';

const mockShowTitle = jest.fn();
const mockShowValidation = jest.fn((message) => message);
const mockShowTitle = vi.fn();
const mockShowValidation = vi.fn((message) => message);

// mock inquirer
const mockPrompt = jest.fn(async function (
const mockPrompt = vi.fn(async function (
questions: DistinctQuestion[],
answers: Answers
) {
Expand Down
6 changes: 2 additions & 4 deletions @commitlint/cz-commitlint/src/Question.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import chalk from 'chalk';
import inquirer, {Answers, InputQuestionOptions} from 'inquirer';

import {jest} from '@jest/globals';

import Question from './Question';
import Question from './Question.js';

const MESSAGES = {
skip: '(press enter to skip)',
Expand Down Expand Up @@ -179,7 +177,7 @@ describe('message', () => {
});

test('should execute function beforeQuestionStart when init message', () => {
const mockFn = jest.fn();
const mockFn = vi.fn();
class CustomQuestion extends Question {
beforeQuestionStart(answers: Answers): void {
mockFn(answers);
Expand Down
5 changes: 3 additions & 2 deletions @commitlint/cz-commitlint/src/SectionBody.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {RuleConfigSeverity} from '@commitlint/types';
import {combineCommitMessage, getQuestions} from './SectionBody';
import {setRules} from './store/rules';

import {combineCommitMessage, getQuestions} from './SectionBody.js';
import {setRules} from './store/rules.js';

describe('getQuestions', () => {
test('should exclude question when body must be empty', () => {
Expand Down
7 changes: 4 additions & 3 deletions @commitlint/cz-commitlint/src/SectionFooter.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {RuleConfigSeverity} from '@commitlint/types';
import {combineCommitMessage, getQuestions} from './SectionFooter';
import {setPromptConfig} from './store/prompts';
import {setRules} from './store/rules';

import {combineCommitMessage, getQuestions} from './SectionFooter.js';
import {setPromptConfig} from './store/prompts.js';
import {setRules} from './store/rules.js';

beforeEach(() => {
setRules({});
Expand Down
7 changes: 4 additions & 3 deletions @commitlint/cz-commitlint/src/SectionHeader.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {RuleConfigSeverity} from '@commitlint/types';

import {
combineCommitMessage,
getQuestions,
getQuestionConfig,
} from './SectionHeader';
import {setPromptConfig} from './store/prompts';
import {setRules} from './store/rules';
} from './SectionHeader.js';
import {setPromptConfig} from './store/prompts.js';
import {setRules} from './store/rules.js';

beforeEach(() => {
setRules({});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {RuleConfigSeverity} from '@commitlint/types';
import {setPromptConfig} from '../store/prompts';
import {setRules} from '../store/rules';
import getRuleQuestionConfig from './getRuleQuestionConfig';

import {setPromptConfig} from '../store/prompts.js';
import {setRules} from '../store/rules.js';
import getRuleQuestionConfig from './getRuleQuestionConfig.js';

// let rules = {};

Expand Down
6 changes: 2 additions & 4 deletions @commitlint/cz-commitlint/src/store/prompts.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {jest} from '@jest/globals';

import * as prompts from './prompts.js';

let getPromptQuestions: typeof prompts.getPromptQuestions;
Expand All @@ -8,7 +6,7 @@ let getPromptSettings: typeof prompts.getPromptSettings;
let setPromptConfig: typeof prompts.setPromptConfig;

beforeEach(async () => {
jest.resetModules();
vi.resetModules();
({getPromptQuestions, getPromptMessages, getPromptSettings, setPromptConfig} =
await import('./prompts.js'));
});
Expand Down Expand Up @@ -118,7 +116,7 @@ describe('setPromptConfig', () => {
});
expect(getPromptSettings()['scopeEnumSeparator']).toEqual('/');

const processExit = jest
const processExit = vi
.spyOn(process, 'exit')
.mockImplementation(() => undefined as never);
setPromptConfig({
Expand Down
5 changes: 3 additions & 2 deletions @commitlint/cz-commitlint/src/store/rules.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {QualifiedRules, RuleConfigSeverity} from '@commitlint/types';
import {GetRuleMethod, SetRulesMethod} from './rules';

import {GetRuleMethod, SetRulesMethod} from './rules.js';

let getRule: GetRuleMethod;
let setRules: SetRulesMethod;

beforeEach(async () => {
jest.resetModules();
vi.resetModules();
({getRule, setRules} = await import('./rules.js'));
});

Expand Down
2 changes: 1 addition & 1 deletion @commitlint/ensure/src/case.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ensure from './case';
import ensure from './case.js';

test('true for no params', () => {
const actual = ensure();
Expand Down
2 changes: 1 addition & 1 deletion @commitlint/ensure/src/enum.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ensure from './enum';
import ensure from './enum.js';

test('false for no params', () => {
const actual = (ensure as () => boolean)();
Expand Down
2 changes: 1 addition & 1 deletion @commitlint/ensure/src/max-length.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ensure from './max-length';
import ensure from './max-length.js';

test('false for no params', () => {
const actual = (ensure as () => boolean)();
Expand Down
2 changes: 1 addition & 1 deletion @commitlint/ensure/src/max-line-length.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ensure from './max-line-length';
import ensure from './max-line-length.js';

test('false for no params', () => {
const actual = (ensure as () => boolean)();
Expand Down
2 changes: 1 addition & 1 deletion @commitlint/ensure/src/min-length.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ensure from './min-length';
import ensure from './min-length.js';

test('false for no params', () => {
const actual = (ensure as () => boolean)();
Expand Down
Loading

0 comments on commit f626df7

Please sign in to comment.