Skip to content

Commit

Permalink
fix(js): should respect vitest test environment
Browse files Browse the repository at this point in the history
closes: nrwl#19853
  • Loading branch information
ndcunningham committed Oct 25, 2023
1 parent 3e0f5c4 commit 7082744
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
28 changes: 28 additions & 0 deletions packages/js/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1359,4 +1359,32 @@ describe('lib', () => {
expect(tree.exists('web/my-lib/src/lib/my-lib.ts')).toBeTruthy();
});
});

describe('--testEnvironment', () => {
it('should generate a vite config with testEnvironment set to node', async () => {
await libraryGenerator(tree, {
...defaultOptions,
name: 'my-node-lib',
unitTestRunner: 'vitest',
testEnvironment: 'node',
});

const content = tree.read('my-node-lib/vite.config.ts', 'utf-8');

expect(content).toContain(`environment: 'node'`);
});

it('should generate a vite config with testEnvironment set to jsdom by default', async () => {
await libraryGenerator(tree, {
...defaultOptions,
name: 'my-jsdom-lib',
unitTestRunner: 'vitest',
testEnvironment: undefined,
});

const content = tree.read('my-jsdom-lib/vite.config.ts', 'utf-8');

expect(content).toContain(`environment: 'jsdom'`);
});
});
});
2 changes: 2 additions & 0 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export async function libraryGeneratorInternal(
project: options.name,
includeLib: true,
includeVitest: options.unitTestRunner === 'vitest',
testEnvironment: options.testEnvironment,
},
false
);
Expand Down Expand Up @@ -136,6 +137,7 @@ export async function libraryGeneratorInternal(
project: options.name,
includeLib: false,
includeVitest: true,
testEnvironment: options.testEnvironment,
},
true
);
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/utils/generator-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ function handleViteConfigFileExists(
cache: {
dir: `${offsetFromRoot}node_modules/.vitest`,
},
environment: 'jsdom',
environment: `${options.testEnvironment ?? 'jsdom'}`,
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
};

Expand Down

0 comments on commit 7082744

Please sign in to comment.