Repository: parcel-bundler/parcel
File: packages/core/integration-tests/test/javascript.js (JavaScript)

Code before:
    );

    let output = await run(b);
    assert.equal(output, 'barbaz');
  });

  it('should insert environment variables from a file even if entry file is specified with source value in package.json', async function () {
    let b = await bundle(
      path.join(
        __dirname,
        '/integration/env-file-with-package-source/index.js',
      ),
      {
        // when the entry file is specified with a source value in package.json and not specified with cli build target value,
        // the entry file is resolved to the entry root directory in packages/core/core/src/resolveOptions.js as InitialParcelOptions.
        // So, entry root directory is specified as InitialParcelOptions' entries value.
        entries: [
          path.join(__dirname, '/integration/env-file-with-package-source'),
        ],
      },
    );

    let output = await run(b);
    assert.equal(output, 'bartest');
  });

  it('should error on process.env mutations', async function () {
    let filePath = path.join(__dirname, '/integration/env-mutate/index.js');
    await assert.rejects(bundle(filePath), {
      diagnostics: [
        {
          origin: '@parcel/transformer-js',
          message: 'Mutating process.env is not supported',
          hints: null,
          codeFrames: [
            {
              filePath,
              codeHighlights: [
                {
                  message: null,
                  start: {
                    line: 1,
                    column: 1,
                  },
                  end: {
                    line: 1,
                    column: 29,
                  },
                },
              ],
            },

Review comment (suggestion):
This test case depends on the implementation of `cli.js`, so it feels muddy. If you know the better way to test this(such as invoking parcel-bundle via CLI instead of `parcel/test-utils/bundle`), please let me know

Code after:
      path.join(__dirname, '/integration/env-disabled/index.js'),
      {
        env: {FOOBAR: 'abc'},
      },
    );

    let output = await run(b);
    assert.ok(!output.toString().includes('process.env'));
    assert.equal(output(), 'undefined:undefined:undefined');
  });

  it('should only insert environment variables in browser environment matching the glob', async function () {
    let b = await bundle(
      path.join(__dirname, '/integration/env-disabled-glob/index.js'),
      {
        env: {A_1: 'abc', B_1: 'def', B_2: 'ghi'},
      },
    );

    let output = await run(b);
    assert.ok(!output.toString().includes('process.env'));
    assert.equal(output(), 'undefined:def:ghi');
  });

  it('should be able to inline environment variables in browser environment', async function () {
    let b = await bundle(path.join(__dirname, '/integration/env/index.js'), {
      env: {NODE_ENV: 'abc'},
    });

    let output = await run(b);
    assert.ok(!output.toString().includes('process.env'));
    assert.equal(output(), 'abc:abc');
  });

  it("should insert the user's NODE_ENV as process.env.NODE_ENV if passed", async function () {
    let b = await bundle(path.join(__dirname, '/integration/env/index.js'), {
      env: {
        NODE_ENV: 'production',
      },
    });

    let output = await run(b);
    assert.ok(!output.toString().includes('process.env'));
    assert.equal(output(), 'production:production');
  });

  it('should not inline computed accesses to process.env', async function () {
    let b = await bundle(
      path.join(__dirname, '/integration/env-computed/index.js'),
      {
        env: {ABC: 'abc'},