diff options
| author | Shinigami <[email protected]> | 2025-06-29 22:56:41 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-29 22:56:41 +0200 |
| commit | a53d5a338d2ca25cba8b76abe085492771ef0bac (patch) | |
| tree | df32f0e45cb1cb6d8d81461991bbf1160de0b021 /test | |
| parent | c7086b21b82d417a1dc2d47ce3bbc26362bb856b (diff) | |
| download | faker-a53d5a338d2ca25cba8b76abe085492771ef0bac.tar.xz faker-a53d5a338d2ca25cba8b76abe085492771ef0bac.zip | |
test: extract cjs require tests to .spec.cts (#3436)
Diffstat (limited to 'test')
| -rw-r--r-- | test/locale-imports.spec.ts | 13 | ||||
| -rw-r--r-- | test/require.spec.cts | 48 | ||||
| -rw-r--r-- | test/simple-faker.spec.ts | 7 |
3 files changed, 52 insertions, 16 deletions
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(); |
