0

Is there a way to disable the strict mode (although I didn't turn it on) for the specific test?

I have a function that pushes the arguments object to an array. In my test, I need to check the array content

(I must use the arguments object as explained here)

Code:

// file.ts

function gtag() {
  window.dataLayer.push(arguments);
}

// Tested function
export function load() {
  gtag('js', new Date());
}

Test:

// file.test.ts

import { export } from './file.ts';

it('should push to the array an arguments object with js and the config', () => {
  // Act    
  load();

  // Assert
  expect(window.dataLayer).toHaveLength(1);
  expect(window.dataLayer[0]).toHaveLength(2);
  expect(window.dataLayer[0][0]).toEqual('js');
  expect(window.dataLayer[0][1]).toEqual(expect.any(Date));

  // Test Fail here:
  expect(window.dataLayer[0]).toHaveProperty('callee');
});

But the test fails with the error:

TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them

Cause I accessed callee in the arguments object.


What I know (and tried):

  • I can't disable strict mode per function as explain here

    but this is running under jest, maybe there is a way to turn it off for certain tests


  • Module code is always strict mode code. From EMCAScript 2015

    But it didn't work even when I:

    • Changed all the code in the code file from export to module.exports
    • Replace import by require in the test file (as suggested here)
    • Added to my tsconfig.json under compilerOptions the options:
      • "strict": false
      • "noImplicitUseStrict": true

If it changes anything, I use TypeScript and this test is in React project but I test pure JS/TS code and don't import any React/other files in my tested file

// index.tsx
ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);
Drew Reese
  • 165,259
  • 14
  • 153
  • 181
Raz Luvaton
  • 3,166
  • 4
  • 21
  • 36

0 Answers0