Skip to content

Commit d5ecf12

Browse files
committed
feat(vitest): support vitest
1 parent 64a6c0e commit d5ecf12

File tree

6 files changed

+43
-6
lines changed

6 files changed

+43
-6
lines changed

src/eslint.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { hasPackage } from './has-package';
2+
13
export const eslintRc = {
24
root: true,
35
env: {
@@ -10,7 +12,7 @@ export const eslintRc = {
1012
'plugin:import/typescript',
1113
'prettier',
1214
],
13-
plugins: ['jest'],
15+
plugins: hasPackage('jest') ? ['jest'] : [],
1416
parserOptions: {
1517
project: './tsconfig.json',
1618
sourceType: 'module',

src/has-package.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function hasPackage(name: string) {
2+
try {
3+
require.resolve(name);
4+
return true;
5+
} catch (error) {
6+
return false;
7+
}
8+
}

src/index.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,44 @@ import type { CoConfigPassthroughEntry } from 'coconfig';
55

66
import { tsconfig, tsconfigBuild } from './tsconfig';
77
import { jestConfig } from './jest';
8+
import { vitestConfig } from './vitest';
89
import { prettierConfig } from './prettier';
910
import { eslintRc } from './eslint';
1011
import { commitLint } from './commitlint';
12+
import { hasPackage } from './has-package';
1113

1214
interface OtiCoConfig {
1315
'.eslintignore': string,
1416
'.npmignore': string,
1517
'tsconfig.json': CoConfigPassthroughEntry<typeof tsconfig>,
1618
'tsconfig.build.json': CoConfigPassthroughEntry<typeof tsconfigBuild>,
17-
'jest.config.js': CoConfigPassthroughEntry,
19+
'jest.config.js'?: CoConfigPassthroughEntry,
20+
'vitest.config.ts'?: CoConfigPassthroughEntry,
1821
'.prettierrc.js': CoConfigPassthroughEntry,
1922
'.eslintrc.js': CoConfigPassthroughEntry,
2023
'.commitlintrc.json': CoConfigPassthroughEntry,
2124
}
2225

23-
export const config: OtiCoConfig = {
26+
const baseConfig: OtiCoConfig = {
2427
'.eslintignore': fs.readFileSync(path.resolve(__dirname, '../templates/eslintignore'), 'utf8'),
2528
'.npmignore': fs.readFileSync(path.resolve(__dirname, '../templates/npmignore'), 'utf8'),
2629
'tsconfig.json': { configuration: tsconfig, stringify: true },
2730
'tsconfig.build.json': { configuration: tsconfigBuild, stringify: true },
28-
'jest.config.js': { configuration: jestConfig },
2931
'.prettierrc.js': { configuration: prettierConfig },
3032
'.eslintrc.js': { configuration: eslintRc },
3133
'.commitlintrc.json': { configuration: commitLint, stringify: true },
3234
};
35+
36+
if (hasPackage('jest')) {
37+
Object.assign(baseConfig, {
38+
'jest.config.js': { configuration: jestConfig },
39+
});
40+
}
41+
42+
if (hasPackage('vitest')) {
43+
Object.assign(baseConfig, {
44+
'vitest.config.ts': { configuration: vitestConfig },
45+
});
46+
}
47+
48+
export const config = baseConfig;

src/jest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ export const jestConfig: JestConfigWithTsJest = {
77
testRegex: '(\\.|/)(test|spec)\\.[jt]sx?$',
88
moduleNameMapper: {
99
'^@/(.*)$': '<rootDir>/$1'
10-
}
10+
},
1111
};

src/tsconfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const tsconfig = {
2-
include: ['@types/**/*', 'src/**/*', '__tests__/**/*', '__mocks__/**/*', 'coconfig.ts'],
2+
include: ['@types/**/*', 'src/**/*', '__tests__/**/*', '__mocks__/**/*', 'coconfig.ts', 'vitest.config.ts'],
33
exclude: ['node_modules', 'build'],
44
'ts-node': {
55
transpileOnly: true,

src/vitest.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function vitestConfig() {
2+
// eslint-disable-next-line @typescript-eslint/no-var-requires
3+
const { configDefaults, defineConfig } = require('vitest/config');
4+
5+
return defineConfig({
6+
test: {
7+
watch: false,
8+
exclude: ['.trunk', ...configDefaults.exclude],
9+
},
10+
});
11+
}

0 commit comments

Comments
 (0)