Skip to content

Commit 0fb1a6a

Browse files
committed
test: extract cjs require tests to .spec.cts
1 parent 50b3241 commit 0fb1a6a

File tree

5 files changed

+55
-18
lines changed

5 files changed

+55
-18
lines changed

eslint.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ const config: ReturnType<typeof tseslint.config> = tseslint.config(
244244
},
245245
{
246246
name: 'test/**/*.ts overrides',
247-
files: ['test/**/*.spec.ts', 'test/**/*.spec.d.ts'],
247+
files: ['test/**/*.spec.ts', 'test/**/*.spec.cts', 'test/**/*.spec.d.ts'],
248248
plugins: {
249249
vitest: eslintPluginVitest,
250250
},

test/locale-imports.spec.ts

-13
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,6 @@ import { allLocales } from '../src';
44
import { keys } from '../src/internal/keys';
55

66
describe.each(keys(allLocales))('locale imports', (locale) => {
7-
it(`should be possible to directly require('@faker-js/faker/locale/${locale}')`, () => {
8-
// eslint-disable-next-line @typescript-eslint/no-require-imports, unicorn/prefer-module
9-
const { faker } = require(`../dist/locale/${locale}.cjs`) as {
10-
faker: Faker;
11-
};
12-
13-
expect(faker).toBeDefined();
14-
expect(faker.string.alpha()).toBeTypeOf('string');
15-
expect(faker.definitions.metadata.title).toBe(
16-
allLocales[locale].metadata?.title
17-
);
18-
});
19-
207
it(`should be possible to directly import('@faker-js/faker/locale/${locale}')`, async () => {
218
const { faker } = (await import(`../dist/locale/${locale}.js`)) as {
229
faker: Faker;

test/require.spec.cts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* eslint-disable @typescript-eslint/no-require-imports, unicorn/prefer-module */
2+
const { describe, expect, it, vi } = await import('vitest');
3+
const { allLocales, SimpleFaker } = require('../dist/index.cjs');
4+
5+
describe('require (cjs)', () => {
6+
describe.each(
7+
Object.keys(
8+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
9+
allLocales
10+
)
11+
)('locale imports', (locale) => {
12+
it(`should be possible to directly require('@faker-js/faker/locale/${locale}')`, () => {
13+
const { faker } = require(`../dist/locale/${locale}.cjs`);
14+
15+
expect(faker).toBeDefined();
16+
expect(faker.string.alpha()).toBeTypeOf('string');
17+
expect(faker.definitions.metadata.title).toBe(
18+
allLocales[locale].metadata?.title
19+
);
20+
});
21+
});
22+
23+
describe('simpleFaker', () => {
24+
it('should not log anything on startup', () => {
25+
const spies = Object.keys(console)
26+
.filter(
27+
(key) =>
28+
// @ts-expect-error: cts cant use `as keyof typeof console`
29+
typeof console[key] === 'function'
30+
)
31+
.map((methodName) =>
32+
vi.spyOn(
33+
console,
34+
// @ts-expect-error: cts cant use `as keyof typeof console`
35+
methodName
36+
)
37+
);
38+
39+
expect(require('..').simpleFaker).toBeDefined();
40+
41+
expect(new SimpleFaker()).toBeDefined();
42+
43+
for (const spy of spies) {
44+
expect(spy).not.toHaveBeenCalled();
45+
spy.mockRestore();
46+
}
47+
});
48+
});
49+
});

test/simple-faker.spec.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import { generateMersenne32Randomizer, SimpleFaker, simpleFaker } from '../src';
44
import { keys } from '../src/internal/keys';
55

66
describe('simpleFaker', () => {
7-
it('should not log anything on startup', () => {
7+
it('should not log anything on startup', async () => {
88
const spies: MockInstance[] = keys(console)
99
.filter((key) => typeof console[key] === 'function')
1010
.map((methodName) => vi.spyOn(console, methodName));
1111

12-
// eslint-disable-next-line @typescript-eslint/no-require-imports, unicorn/prefer-module -- Using import() requires types being build but the CI / TS-Check runs without them.
13-
expect(require('..').simpleFaker).toBeDefined();
12+
// Using import() requires types being build but the CI / TS-Check runs without them.
13+
const { simpleFaker: importedSimpleFaker } = await import('..');
14+
expect(importedSimpleFaker).toBeDefined();
1415

1516
expect(new SimpleFaker()).toBeDefined();
1617

vitest.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ console.log('VITEST_SEQUENCE_SEED', VITEST_SEQUENCE_SEED);
99
export default defineConfig({
1010
test: {
1111
setupFiles: ['test/setup.ts'],
12-
include: ['test/**/*.spec.ts'],
12+
include: ['test/**/*.spec.ts', 'test/**/*.spec.cts'],
1313
exclude: ['test/integration/**/*.spec.ts'],
1414
coverage: {
1515
all: true,

0 commit comments

Comments
 (0)