diff --git a/config/scripts/postinstall.js b/config/scripts/postinstall.js index fe3055201..c1f5cbeed 100755 --- a/config/scripts/postinstall.js +++ b/config/scripts/postinstall.js @@ -7,8 +7,7 @@ const { execSync } = require('child_process') // NPM stores the parent project directory in the "INIT_CWD" env variable. const parentPackageCwd = process.env.INIT_CWD -function postinstall() { - // 1. Check if "package.json" has "msw.workerDirectory" property set. +function postInstall() { const packageJson = JSON.parse( fs.readFileSync(path.resolve(parentPackageCwd, 'package.json'), 'utf8'), ) @@ -19,39 +18,19 @@ function postinstall() { const cliExecutable = path.resolve(process.cwd(), 'cli/index.js') - // 2. Check if the worker directory is an existing path. - const workerDirectories = Array.prototype.concat( - packageJson.msw.workerDirectory, - ) - - for (const workerDirectory of workerDirectories) { - const absoluteWorkerDirectory = path.resolve( - parentPackageCwd, - workerDirectory, + try { + /** + * @note Call the "init" command directly. It will now copy the worker script + * to all saved paths in "msw.workerDirectory" + */ + execSync(`node ${cliExecutable} init`, { + cwd: parentPackageCwd, + }) + } catch (error) { + console.error( + `[MSW] Failed to automatically update the worker script.\n\n${error}`, ) - - console.log({ absoluteWorkerDirectory }) - - if (!fs.existsSync(absoluteWorkerDirectory)) { - console.warn( - `[MSW] Failed to automatically update the worker script at "%s": given path does not exist.`, - workerDirectory, - ) - continue - } - - // 3. Update the worker script. - try { - execSync(`node ${cliExecutable} init ${absoluteWorkerDirectory}`, { - cwd: parentPackageCwd, - }) - } catch (error) { - console.error( - `[MSW] Failed to automatically update the worker script at "${absoluteWorkerDirectory}":\n${error}`, - ) - continue - } } } -postinstall() +postInstall() diff --git a/test/node/msw-api/auto-update-worker.node.test.ts b/test/node/msw-api/auto-update-worker.node.test.ts index aecc0b9aa..47b69b69f 100644 --- a/test/node/msw-api/auto-update-worker.node.test.ts +++ b/test/node/msw-api/auto-update-worker.node.test.ts @@ -11,21 +11,21 @@ const fsMock = createTeardown({ rootDir: fromTemp('auto-update-worker'), }) -beforeAll(async () => { - await fsMock.prepare() -}) +describe.sequential( + 'worker script auto-update', + () => { + beforeAll(async () => { + await fsMock.prepare() + }) -afterEach(async () => { - // await fsMock.reset() -}) + afterEach(async () => { + await fsMock.reset() + }) -afterAll(async () => { - await fsMock.cleanup() -}) + afterAll(async () => { + await fsMock.cleanup() + }) -describe.sequential( - 'worker script auto-updates', - () => { test('updates the worker script on the postinstall hook', async () => { await fsMock.create({ 'package.json': JSON.stringify({ @@ -71,7 +71,6 @@ describe.sequential( `npm install msw-${packageJson.version}.tgz`, ) expect(installCommand.stderr).toBe('') - console.log(installCommand.stdout) expect( fs.existsSync(fsMock.resolve('packages/one/mockServiceWorker.js')), @@ -81,5 +80,8 @@ describe.sequential( ).toEqual(true) }) }, - { timeout: 60_000 }, + { + // These tests actually build, pack, and install MSW so they may take time. + timeout: 60_000, + }, )