Javascript Jest遇到意外标记:实现try/catch
我得到以下错误: Jest遇到了意外的标记 }抓住{Javascript Jest遇到意外标记:实现try/catch,javascript,jestjs,babeljs,babel-jest,Javascript,Jestjs,Babeljs,Babel Jest,我得到以下错误: Jest遇到了意外的标记 }抓住{ ^ 我假设我需要babel来转换我导入的文件,但我不明白jest/babel是如何连接在一起的。我如何让它转换我导入的文件,以便我可以尝试/catch 我有以下代码: babel.config.js module.exports = { presets: [ [ '@babel/preset-env', { targets: {
^
我假设我需要babel来转换我导入的文件,但我不明白jest/babel是如何连接在一起的。我如何让它转换我导入的文件,以便我可以尝试/catch
我有以下代码:
babel.config.js
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
};
package.json
{
"name": "tests",
"scripts": {
"test": "jest"
},
"author": "jonny-b",
"dependencies": {
"jest": "^24.8.0",
"ruby-script": "^1.0.3"
}
}
index.js
class Collection extends Array {
constructor(array) {
super(array.length);
Object.assign(this, array);
}
//....
drill(...indices) {
if (this[indices] === null) return null;
if (indices.length === 1) return this[indices];
let indexes = indices.splice(indices[0]);
try {
let collection = new Collection(this[indices[0]]);
return collection.drill(...indexes);
} catch {
throw `${typeof (this[indices[0]])}`
}
}
}
script.test.js
let Collection = require('<path to file>/index.js');
describe('ruby-script', () => {
it('should error if called on a non collection return value', () => {
let collection = new Collection([1, [2, [3, [4, [5, [6, [7]]]]]]]);
expect(collection.dig(1)).toEqual(true)
})
}
let Collection=require('/index.js');
描述('ruby-script',()=>{
它('如果对非集合返回值调用,则应出错',()=>{
let collection=新集合([1、[2、[3、[4、[5、[6、[7]]]);
expect(collection.dig(1)).toEqual(true)
})
}