使用袋鼠进行测试时出现意外的保留字错误
在我编写测试用例的测试文件中,我导入了如下所示的打字稿文件:
import {rootReducer} from "../src/reducers/rootReducer";
在rootReducer.ts中,我导入了另一个类似于下面的打印文件:
import taskReducer from "./taskReducer.ts";
然后它显示错误:
SyntaxError: Unexpected reserved word
at src/reducers/rootReducer.ts:7
rootReducer.ts和taskReducer.ts都位于文件夹/ src / reducers下
如果您从导入语句中删除'.ts',但在浏览器中抛出错误,则不会失败测试。 该应用程序不会运行
袋鼠配置如下:
module.exports = function (wallaby) {
    return {
        files: [
            'src/*.ts',
            'src/**/*.ts'
        ],
        tests: [
            'test/*Test.ts'
        ],
        testFramework: "mocha",
        env: {
            type: 'node'
        },
        compilers: {
            '**/*.ts': wallaby.compilers.typeScript({
                /* 1 for CommonJs*/
                module: 1
            })
        }
    }
};
您的声明:
import taskReducer from "./taskReducer.ts";
应该是:
// Import just taskReducer from this module
import {taskReducer} from "./taskReducer";
要么:
// Import the whole module and call it taskReducer
import * as taskReducer from "./taskReducer";
这个问题不在wallaby.js中,而是在你的webpack配置中。 要启用需要的文件而不指定扩展名,您必须添加一个resolve.extensions参数,以指定webpack搜索哪些文件:
// webpack.config.js
module.exports = {
  ...
  resolve: {
    // you can now require('file') instead of require('file.ts')
    extensions: ['', '.js', '.ts', '.tsx'] 
  }
};
上一篇: Unexpected reserved word error while testing using wallaby
