Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/core/src/runtime/worker/loadModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ const createRequire = (
assetFiles: Record<string, string>,
interopDefault: boolean,
): NodeJS.Require => {
const _require = createNativeRequire(filename);
const _require = (() => {
try {
// compat with some testPath may not be an available path but the third-party package name
return createNativeRequire(filename);
} catch (_err) {
return createNativeRequire(distPath);
}
})();

const require = ((id: string) => {
const currentDirectory = path.dirname(distPath);

Expand Down
33 changes: 25 additions & 8 deletions packages/core/src/utils/testFiles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { existsSync } from 'node:fs';
import fs from 'node:fs/promises';
import { createRequire } from 'node:module';
import pathe from 'pathe';
import { glob } from 'tinyglobby';
import { castArray, color, getAbsolutePath, parsePosix } from './helper';
Expand Down Expand Up @@ -98,23 +99,39 @@ export const getTestEntries = async ({
);
};

const tryResolve = (request: string, rootPath: string) => {
try {
const require = createRequire(rootPath);
return require.resolve(request, { paths: [rootPath] });
} catch (_err) {
return undefined;
}
};

export const getSetupFiles = (
setups: string[] | string | undefined,
rootPath: string,
): Record<string, string> => {
return Object.fromEntries(
castArray(setups).map((setupFile) => {
const setupFilePath = getAbsolutePath(rootPath, setupFile);

if (!existsSync(setupFilePath)) {
let errorMessage = `Setup file ${color.red(setupFile)} not found`;
if (setupFilePath !== setupFile) {
errorMessage += color.gray(` (resolved path: ${setupFilePath})`);
try {
if (!existsSync(setupFilePath)) {
let errorMessage = `Setup file ${color.red(setupFile)} not found`;
if (setupFilePath !== setupFile) {
errorMessage += color.gray(` (resolved path: ${setupFilePath})`);
}
throw errorMessage;
}
const relativePath = pathe.relative(rootPath, setupFilePath);
return [formatTestEntryName(relativePath), setupFilePath];
} catch (err) {
// support use package name as setupFiles value
if (tryResolve(setupFile, rootPath)) {
return [formatTestEntryName(setupFile), setupFile];
}
throw errorMessage;
throw err;
}
const relativePath = pathe.relative(rootPath, setupFilePath);
return [formatTestEntryName(relativePath), setupFilePath];
}),
);
};
Expand Down
6 changes: 6 additions & 0 deletions tests/setup/fixtures/package-name/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { expect, it } from '@rstest/core';

it('should run setup file correctly', () => {
expect(process.env.RETEST_SETUP_FLAG).toBe('1');
expect(process.env.NODE_ENV).toBe('rstest:production');
});
15 changes: 15 additions & 0 deletions tests/setup/fixtures/package-name/rstest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { join } from 'node:path';
import { defineConfig } from '@rstest/core';

import fse from 'fs-extra';

fse.copySync(
join(__dirname, './test-setup-fixtures'),
join(__dirname, './node_modules/test-setup'),
);

export default defineConfig({
passWithNoTests: true,
setupFiles: ['test-setup'],
exclude: ['**/node_modules/**', '**/dist/**'],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
process.env.RETEST_SETUP_FLAG = '1';
process.env.NODE_ENV = 'rstest:production';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"name": "@rstest/tests-setup",
"main": "index.js",
"version": "1.0.0"
}
13 changes: 13 additions & 0 deletions tests/setup/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,17 @@ describe('test setup file', async () => {
logs.find((log) => log.includes('rstest.setup.ts:1:7')),
).toBeTruthy();
});

it('should run setup file correctly when setupFiles value is package name', async () => {
const { expectExecSuccess } = await runRstestCli({
command: 'rstest',
args: ['run'],
options: {
nodeOptions: {
cwd: join(__dirname, 'fixtures/package-name'),
},
},
});
await expectExecSuccess();
});
});
Loading