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

Code before:

  it('should overwrite environment variables from a file if passed', async function () {
    let b = await bundle(
      path.join(__dirname, '/integration/env-file/index.js'),
      {env: {BAR: 'baz'}},
    );

    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,

Review comment (suggestion):
You don't need that entries setting if you just pass the directory as an entry directly instead of the index file.
```suggestion
    let b = await bundle(
      path.join(__dirname, '/integration/env-file-with-package-source'),
    );
```

Code after:
    assert.equal(output(), 'test:undefined');
  });

  it('should not insert environment variables in browser environment if disabled', async function () {
    let b = await bundle(
      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');
  });