aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShinigami <[email protected]>2025-06-29 22:56:41 +0200
committerGitHub <[email protected]>2025-06-29 22:56:41 +0200
commita53d5a338d2ca25cba8b76abe085492771ef0bac (patch)
treedf32f0e45cb1cb6d8d81461991bbf1160de0b021
parentc7086b21b82d417a1dc2d47ce3bbc26362bb856b (diff)
downloadfaker-a53d5a338d2ca25cba8b76abe085492771ef0bac.tar.xz
faker-a53d5a338d2ca25cba8b76abe085492771ef0bac.zip
test: extract cjs require tests to .spec.cts (#3436)
-rw-r--r--eslint.config.ts9
-rw-r--r--test/locale-imports.spec.ts13
-rw-r--r--test/require.spec.cts48
-rw-r--r--test/simple-faker.spec.ts7
-rw-r--r--vitest.config.ts2
5 files changed, 61 insertions, 18 deletions
diff --git a/eslint.config.ts b/eslint.config.ts
index cc0fc72a..26eec55a 100644
--- a/eslint.config.ts
+++ b/eslint.config.ts
@@ -256,7 +256,7 @@ const config: ReturnType<typeof tseslint.config> = tseslint.config(
},
{
name: 'test/**/*.ts overrides',
- files: ['test/**/*.spec.ts', 'test/**/*.spec.d.ts'],
+ files: ['test/**/*.spec.ts', 'test/**/*.spec.cts', 'test/**/*.spec.d.ts'],
plugins: {
vitest: eslintPluginVitest,
},
@@ -285,6 +285,13 @@ const config: ReturnType<typeof tseslint.config> = tseslint.config(
typecheck: true,
},
},
+ },
+ {
+ files: ['test/**/*.spec.cts'],
+ rules: {
+ '@typescript-eslint/no-require-imports': 'off',
+ 'unicorn/prefer-module': 'off',
+ },
}
//#endregion
);
diff --git a/test/locale-imports.spec.ts b/test/locale-imports.spec.ts
index 71d73ec0..6813dfe7 100644
--- a/test/locale-imports.spec.ts
+++ b/test/locale-imports.spec.ts
@@ -5,19 +5,6 @@ import { allLocales } from '../src';
import { keys } from '../src/internal/keys';
describe.each(keys(allLocales))('locale imports', (locale) => {
- it(`should be possible to directly require('@faker-js/faker/locale/${locale}')`, () => {
- // eslint-disable-next-line @typescript-eslint/no-require-imports, unicorn/prefer-module
- const { faker } = require(`../dist/locale/${locale}.cjs`) as {
- faker: Faker;
- };
-
- expect(faker).toBeDefined();
- expect(faker.string.alpha()).toBeTypeOf('string');
- expect(faker.definitions.metadata.title).toBe(
- allLocales[locale].metadata?.title
- );
- });
-
it(`should be possible to directly import('@faker-js/faker/locale/${locale}')`, async () => {
const { faker } = (await import(`../dist/locale/${locale}.js`)) as {
faker: Faker;
diff --git a/test/require.spec.cts b/test/require.spec.cts
new file mode 100644
index 00000000..80f708d9
--- /dev/null
+++ b/test/require.spec.cts
@@ -0,0 +1,48 @@
+const { describe, expect, it, vi } = await import('vitest');
+const { allLocales, SimpleFaker } = require('../dist/index.cjs');
+
+describe('require (cjs)', () => {
+ describe.each(
+ Object.keys(
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
+ allLocales
+ )
+ )('locale imports', (locale) => {
+ it(`should be possible to directly require('@faker-js/faker/locale/${locale}')`, () => {
+ const { faker } = require(`../dist/locale/${locale}.cjs`);
+
+ expect(faker).toBeDefined();
+ expect(faker.string.alpha()).toBeTypeOf('string');
+ expect(faker.definitions.metadata.title).toBe(
+ allLocales[locale].metadata?.title
+ );
+ });
+ });
+
+ describe('simpleFaker', () => {
+ it('should not log anything on startup', () => {
+ const spies = Object.keys(console)
+ .filter(
+ (key) =>
+ // @ts-expect-error: cts cant use `as keyof typeof console`
+ typeof console[key] === 'function'
+ )
+ .map((methodName) =>
+ vi.spyOn(
+ console,
+ // @ts-expect-error: cts cant use `as keyof typeof console`
+ methodName
+ )
+ );
+
+ expect(require('..').simpleFaker).toBeDefined();
+
+ expect(new SimpleFaker()).toBeDefined();
+
+ for (const spy of spies) {
+ expect(spy).not.toHaveBeenCalled();
+ spy.mockRestore();
+ }
+ });
+ });
+});
diff --git a/test/simple-faker.spec.ts b/test/simple-faker.spec.ts
index 02c860a5..c1470624 100644
--- a/test/simple-faker.spec.ts
+++ b/test/simple-faker.spec.ts
@@ -4,13 +4,14 @@ import { generateMersenne32Randomizer, SimpleFaker, simpleFaker } from '../src';
import { keys } from '../src/internal/keys';
describe('simpleFaker', () => {
- it('should not log anything on startup', () => {
+ it('should not log anything on startup', async () => {
const spies: MockInstance[] = keys(console)
.filter((key) => typeof console[key] === 'function')
.map((methodName) => vi.spyOn(console, methodName));
- // 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.
- expect(require('..').simpleFaker).toBeDefined();
+ // Using import() requires types being build but the CI / TS-Check runs without them.
+ const { simpleFaker: importedSimpleFaker } = await import('..');
+ expect(importedSimpleFaker).toBeDefined();
expect(new SimpleFaker()).toBeDefined();
diff --git a/vitest.config.ts b/vitest.config.ts
index 1dda2df9..5f0c03be 100644
--- a/vitest.config.ts
+++ b/vitest.config.ts
@@ -9,7 +9,7 @@ console.log('VITEST_SEQUENCE_SEED', VITEST_SEQUENCE_SEED);
export default defineConfig({
test: {
setupFiles: ['test/setup.ts'],
- include: ['test/**/*.spec.ts'],
+ include: ['test/**/*.spec.ts', 'test/**/*.spec.cts'],
exclude: ['test/integration/**/*.spec.ts'],
coverage: {
all: true,