Skip to content

Commit 10734a1

Browse files
committed
experiment
1 parent 8fd8132 commit 10734a1

File tree

2 files changed

+28
-35
lines changed

2 files changed

+28
-35
lines changed

src/config/jest-playwright.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,8 @@ const getTestRunnerPath = () => process.env.STORYBOOK_TEST_RUNNER_PATH ?? '@stor
1919
* */
2020
const getJestPlaywrightConfig = (): Config.InitialOptions => {
2121
const TEST_RUNNER_PATH = getTestRunnerPath();
22-
const presetBasePath = path.dirname(
23-
require.resolve('jest-playwright-preset', {
24-
paths: [path.join(import.meta.dirname, '../node_modules')],
25-
})
26-
);
27-
const expectPlaywrightPath = path.dirname(
28-
require.resolve('expect-playwright', {
29-
paths: [path.join(import.meta.dirname, '../node_modules')],
30-
})
31-
);
22+
const presetBasePath = path.dirname(require.resolve('jest-playwright-preset'));
23+
const expectPlaywrightPath = path.dirname(require.resolve('expect-playwright'));
3224
return {
3325
runner: path.join(presetBasePath, 'runner.js'),
3426
globalSetup: require.resolve(`${TEST_RUNNER_PATH}/playwright/global-setup.js`),
@@ -52,17 +44,9 @@ export const getJestConfig = (): Config.InitialOptions => {
5244
STORYBOOK_JUNIT,
5345
} = process.env;
5446

55-
const jestJunitPath = path.dirname(
56-
require.resolve('jest-junit', {
57-
paths: [path.join(import.meta.dirname, '../node_modules')],
58-
})
59-
);
47+
const jestJunitPath = path.dirname(require.resolve('jest-junit'));
6048

61-
const jestSerializerHtmlPath = path.dirname(
62-
require.resolve('jest-serializer-html', {
63-
paths: [path.join(import.meta.dirname, '../node_modules')],
64-
})
65-
);
49+
const jestSerializerHtmlPath = path.dirname(require.resolve('jest-serializer-html'));
6650

6751
// const swcJestPath = path.dirname(
6852
// require.resolve('@swc/jest', {

src/test-storybook.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import { execSync } from 'child_process';
55
import fetch from 'node-fetch';
66
import { canBindToHost } from 'can-bind-to-host';
77
import dedent from 'ts-dedent';
8-
import path, { join, resolve } from 'path';
8+
import path from 'path';
9+
import { join, resolve } from 'path';
910
import tempy from 'tempy';
11+
import { fileURLToPath } from 'url';
12+
import { createRequire } from 'module';
1013
// @ts-ignore
1114
import { getInterpretedFile } from 'storybook/internal/common';
1215
// @ts-ignore
@@ -15,11 +18,16 @@ import { readConfig } from 'storybook/internal/csf-tools';
1518
import { telemetry } from 'storybook/internal/telemetry';
1619
import { glob } from 'glob';
1720

18-
import { JestOptions, getCliOptions } from './util/getCliOptions';
19-
import { getStorybookMetadata } from './util/getStorybookMetadata';
20-
import { getTestRunnerConfig } from './util/getTestRunnerConfig';
21-
import { transformPlaywrightJson } from './playwright/transformPlaywrightJson';
22-
import { TestRunnerConfig } from './playwright/hooks';
21+
import { JestOptions, getCliOptions } from './util/getCliOptions.js';
22+
import { getStorybookMetadata } from './util/getStorybookMetadata.js';
23+
import { getTestRunnerConfig } from './util/getTestRunnerConfig.js';
24+
import { transformPlaywrightJson } from './playwright/transformPlaywrightJson.js';
25+
import { TestRunnerConfig } from './playwright/hooks.js';
26+
27+
// Get the current file's directory in ESM
28+
const __filename2 = fileURLToPath(import.meta.url);
29+
const __dirname = path.dirname(__filename2);
30+
const require = createRequire(import.meta.url);
2331

2432
// Do this as the first thing so that any code reading it knows the right env.
2533
process.env.BABEL_ENV = 'test';
@@ -51,10 +59,11 @@ const cleanup = () => {
5159
}
5260
};
5361

54-
function getNycBinPath() {
55-
const nycPath = path.join(require.resolve('nyc/package.json'));
56-
const nycBin = require(nycPath).bin.nyc;
57-
const nycBinFullPath = path.join(path.dirname(nycPath), nycBin);
62+
async function getNycBinPath() {
63+
const nycPkgUrl = new URL('nyc/package.json', import.meta.url);
64+
const { bin } = await import(nycPkgUrl.href, { assert: { type: 'json' } });
65+
const nycBin = bin.nyc;
66+
const nycBinFullPath = path.join(path.dirname(fileURLToPath(nycPkgUrl)), nycBin);
5867
return nycBinFullPath;
5968
}
6069

@@ -83,7 +92,7 @@ async function reportCoverage() {
8392
// --check-coverage if we want to break if coverage reaches certain threshold
8493
// .nycrc will be respected for thresholds etc. https://www.npmjs.com/package/nyc#coverage-thresholds
8594
if (process.env.JEST_SHARD !== 'true') {
86-
const nycBinFullPath = getNycBinPath();
95+
const nycBinFullPath = await getNycBinPath();
8796
execSync(
8897
`node ${nycBinFullPath} report --reporter=text --reporter=lcov -t ${coverageFolder} --report-dir ${coverageFolder}`,
8998
{
@@ -129,10 +138,10 @@ async function executeJestPlaywright(args: JestOptions) {
129138
// Always prefer jest installed via the test runner. If it's hoisted, it will get it from root node_modules
130139
const jestPath = path.dirname(
131140
require.resolve('jest', {
132-
paths: [path.join(import.meta.dirname, '../@storybook/test-runner/node_modules')],
141+
paths: [path.join(__dirname, '../@storybook/test-runner/node_modules')],
133142
})
134143
);
135-
const jest = await import(jestPath);
144+
const { default: jest } = await import(path.join(jestPath, 'index.js'));
136145
const argv = args.slice(2);
137146

138147
// jest configs could either come in the root dir, or inside of the Storybook config dir
@@ -146,7 +155,7 @@ async function executeJestPlaywright(args: JestOptions) {
146155

147156
const jestConfigPath =
148157
userDefinedJestConfig ||
149-
path.resolve(import.meta.dirname, path.join('..', 'playwright', 'test-runner-jest.config.js'));
158+
path.resolve(__dirname, path.join('..', 'playwright', 'test-runner-jest.config.js'));
150159

151160
argv.push('--config', jestConfigPath);
152161

@@ -235,7 +244,7 @@ async function getIndexTempDir(url: string) {
235244
}
236245

237246
function ejectConfiguration() {
238-
const origin = path.resolve(import.meta.dirname, '../playwright/test-runner-jest.config.js');
247+
const origin = path.resolve(__dirname, '../playwright/test-runner-jest.config.js');
239248
const destination = path.resolve('test-runner-jest.config.js');
240249
const fileAlreadyExists = fs.existsSync(destination);
241250

0 commit comments

Comments
 (0)