javascript - 如何在没有严格模式的情况下运行特定测试或所有内容
问题描述
有没有办法为特定测试禁用严格模式(尽管我没有打开它)?
我有一个将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
因为我callee
在arguments
对象中访问过。
我知道(并尝试过):
我无法按照此处的说明禁用每个功能的严格模式
但这是开玩笑的,也许有办法在某些测试中关闭它
模块代码始终是严格模式代码。来自EMCAScript 2015
但即使我:
- 将代码文件中的所有代码从更改
export
为module.exports
- 在测试文件中替换
import
为require
(如建议here) - 添加到我的选项
tsconfig.json
下compilerOptions
:"strict": false
"noImplicitUseStrict": true
- 将代码文件中的所有代码从更改
如果它改变了任何东西,我使用 TypeScript,这个测试在 React 项目中,但我测试纯 JS/TS 代码并且不在我的测试文件中导入任何 React/其他文件
// index.tsx
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
解决方案
推荐阅读
- python-3.x - 用 matplotlib 绘制一个圆锥
- java - 使用多个源参数的 Mapstruct 表达式映射
- statistics - 具有恒定基年的增长公式
- docker - 无法重新启动单个节点 etcd,其中 data_dir 映射到 kubernetes 中的持久卷
- spring - 如何将弹簧安全设置从弹簧转移到弹簧靴
- jquery-ui - jquery ui-sortable 事件,beforeStop 得到错误的 index() 值
- java - 您可以扩展 IntSummaryStatistics 类吗?
- java - 匿名可运行实现并将其发布到处理程序
- javascript - 有没有办法在移动设备上强制重新加载来自不同来源的 iframe?
- javascript - 在排序另一个数组之前复制数组的顺序