首页 > 解决方案 > 如何在没有严格模式的情况下运行特定测试或所有内容

问题描述

有没有办法为特定测试禁用严格模式(尽管我没有打开它)?

我有一个将arguments对象推送到数组的函数。在我的测试中,我需要检查数组内容

(我必须使用这里arguments解释的对象)

代码:

// file.ts

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

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

测试:

// 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');
});

但是测试失败并出现错误:

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

因为我calleearguments对象中访问过。


我知道(并尝试过):



如果它改变了任何东西,我使用 TypeScript,这个测试在 React 项目中,但我测试纯 JS/TS 代码并且不在我的测试文件中导入任何 React/其他文件

// index.tsx
ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

标签: javascripttypescriptjestjsstrict

解决方案


推荐阅读