@@ -5,8 +5,11 @@ import { execSync } from 'child_process';
55import fetch from 'node-fetch' ;
66import { canBindToHost } from 'can-bind-to-host' ;
77import dedent from 'ts-dedent' ;
8- import path , { join , resolve } from 'path' ;
8+ import path from 'path' ;
9+ import { join , resolve } from 'path' ;
910import tempy from 'tempy' ;
11+ import { fileURLToPath } from 'url' ;
12+ import { createRequire } from 'module' ;
1013// @ts -ignore
1114import { getInterpretedFile } from 'storybook/internal/common' ;
1215// @ts -ignore
@@ -15,11 +18,16 @@ import { readConfig } from 'storybook/internal/csf-tools';
1518import { telemetry } from 'storybook/internal/telemetry' ;
1619import { 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.
2533process . 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
237246function 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