Repository: facebook/jest
File: e2e/native-esm/__tests__/native-esm.test.js (JavaScript)

Code before:

test('can unmock module', async () => {
  jestObject.unstable_mockModule('../index.js', () => ({
    double: num => num * 1000,
  }));

  const importedMock = await import('../index.js');
  expect(importedMock.double(2)).toBe(2000);

  jestObject.unstable_unmockModule('../index.js');

  const importedMockAfterUnmock = await import('../index.js');
  expect(importedMockAfterUnmock.double(2)).toBe(4);
});

test('can unmock transitive module', async () => {
  jestObject.unstable_mockModule('../index.js', () => ({
    double: num => num * 1000,
  }));

  const importedMock = await import('../reexport.js');
  expect(importedMock.double(2)).toBe(2000);

  jestObject.unstable_unmockModule('../index.js');

  const importedMockAfterUnmock = await import('../reexport.js');
  expect(importedMockAfterUnmock.double(2)).toBe(4);
});

test('can unmock module; don`t override mock', async () => {
  jestObject.unstable_mockModule('../index.js', () => ({
    double: num => num * 1000,
  }));

  const importedMock = await import('../index.js');
  expect(importedMock.double(2)).toBe(2000);

  jestObject.unstable_unmockModule('../index.js');

  const importedMockAfterUnmock = await import('../index.js');
  expect(importedMockAfterUnmock.double(2)).toBe(4);

  jestObject.unstable_mockModule('../index.js', () => ({
    double: () => 'never call',
  }));

  const importedMockAfterOverride = await import('../index.js');
  expect(importedMockAfterOverride.double(2)).not.toBe('never call');
  expect(importedMockAfterOverride.double(2)).toBe(2000);
});


Review comment (suggestion):
I don't think this will work as `../reexport.js` already has a reference to the old (mocked) `index.js`.

Do you have an example of a test of ours using `unmock` that works in this way? If not, I think you can just delete the test

Code after:

test('supports imports from "data:text/javascript" URI without explicit encoding', async () => {
  const code = 'export const something = "some value"';
  const importedEncoded = await import(
    `data:text/javascript,${encodeURIComponent(code)}`
  );
  expect(importedEncoded.something).toBe('some value');
});

test('imports from "data:text/javascript" URI with invalid encoding fail', async () => {
  const code = 'export const something = "some value"';
  await expect(
    () =>
      import(
        `data:text/javascript;charset=badEncoding,${encodeURIComponent(code)}`
      ),
  ).rejects.toThrow('Invalid data URI');
});

test('imports from "data:" URI with invalid mime type fail', async () => {
  const code = 'export const something = "some value"';
  await expect(
    () => import(`data:something/else,${encodeURIComponent(code)}`),
  ).rejects.toThrow('Invalid data URI');
});

test('imports from "data:text/javascript" URI with invalid data fail', async () => {
  await expect(
    () => import('data:text/javascript;charset=utf-8,so(me)+.-gibberish'),
  ).rejects.toThrow("Unexpected token '.'");
});

test('supports imports from "data:application/json" URI', async () => {
  const data = await import('data:application/json,{"foo": "bar"}');
  expect(data.default).toEqual({foo: 'bar'});
});

test('supports static "data:" URI import', async () => {
  const module = await import('../staticDataImport.js');
  expect(module.value()).toEqual({bar: {obj: 456}, foo: '123'});
});

test('imports from "data:" URI is properly cached', async () => {
  const code =
    'export const wrapper = {value: 123}\nexport const set = (value) => wrapper.value = value';
  const data1 = await import(
    `data:text/javascript;charset=utf-8,${encodeURIComponent(code)}`
  );
  expect(data1.wrapper.value).toBe(123);
  data1.set(234);
  expect(data1.wrapper.value).toBe(234);