Repository: nodejs/node
File: test/sequential/test-watch-mode.mjs (JavaScript)

Code before:

  it('should support multiple --env-file flags', async () => {
    const envKey = `TEST_ENV_A_${Date.now()}`;
    const envKey2 = `TEST_ENV_B_${Date.now()}`;
    const jsFile = createTmpFile(`console.log('ENV_A: ' + process.env.${envKey} + '\\n' + 'ENV_B: ' + process.env.${envKey2});`);
    const envFileA = createTmpFile(`${envKey}=123`, '.env');
    const envFileB = createTmpFile(`${envKey2}=456`, '.env');
    const { done, restart } = runInBackground({
      args: ['--watch', `--env-file=${envFileA}`, `--env-file=${envFileB}`, jsFile],
    });

    try {
      const { stderr, stdout } = await restart();

      assert.strictEqual(stderr, '');
      assert.deepStrictEqual(stdout, [
        'ENV_A: 123',
        'ENV_B: 456',
        `Completed running ${inspect(jsFile)}. Waiting for file changes before restarting...`,
      ]);
    } finally {
      await done();
    }
  });

  it('should watch changes even when there is syntax errors during esm loading', async () => {
    // Create initial file with valid code
    const initialContent = `console.log('hello, world');`;
    const file = createTmpFile(initialContent, '.mjs');

    const { done, restart } = runInBackground({
      args: ['--watch', file],
      completed: 'Completed running',
      shouldFail: true,
    });

    try {
      const { stdout, stderr } = await restart();
      assert.strictEqual(stderr, '');
      assert.deepStrictEqual(stdout, [
        'hello, world',
        `Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
      ]);

      // Update file with syntax error
      const syntaxErrorContent = `console.log('hello, wor`;
      writeFileSync(file, syntaxErrorContent);

      // Wait for the failed restart
      const { stderr: stderr2, stdout: stdout2 } = await restart();
      assert.match(stderr2, /SyntaxError: Invalid or unexpected token/);

Review comment (bug):
Can you add the test in a separate file instead of appending it to a test monolith? See https://github.com/nodejs/node/blob/main/doc/contributing/writing-tests.md#how-to-write-a-good-test - a monolithic test increase the chances of flakes in the CI and makes flakes even harder to debug due to the enormous output.

Code after:

  it('should support multiple --env-file flags', async () => {
    const envKey = `TEST_ENV_A_${Date.now()}`;
    const envKey2 = `TEST_ENV_B_${Date.now()}`;
    const jsFile = createTmpFile(`console.log('ENV_A: ' + process.env.${envKey} + '\\n' + 'ENV_B: ' + process.env.${envKey2});`);
    const envFileA = createTmpFile(`${envKey}=123`, '.env');
    const envFileB = createTmpFile(`${envKey2}=456`, '.env');
    const { done, restart } = runInBackground({
      args: ['--watch', `--env-file=${envFileA}`, `--env-file=${envFileB}`, jsFile],
    });

    try {
      const { stderr, stdout } = await restart();

      assert.strictEqual(stderr, '');
      assert.deepStrictEqual(stdout, [
        'ENV_A: 123',
        'ENV_B: 456',
        `Completed running ${inspect(jsFile)}. Waiting for file changes before restarting...`,
      ]);
    } finally {
      await done();
    }
  });
});