Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Intern v4测试ES2015课程_Javascript_Unit Testing_Intern - Fatal编程技术网

Javascript 使用Intern v4测试ES2015课程

Javascript 使用Intern v4测试ES2015课程,javascript,unit-testing,intern,Javascript,Unit Testing,Intern,如何使用InternV4导入类并编写单元测试。我一直遇到一个错误语法错误:意外的令牌导入 单元测试文件demo.js: import Demo from '../../app/demo.js'; const { suite, test } = intern.getInterface('tdd'); const { assert } = intern.getPlugin('chai'); const { expect } = intern.getPlugin('chai'); Demo.js类

如何使用InternV4导入类并编写单元测试。我一直遇到一个错误语法错误:意外的令牌导入

单元测试文件demo.js:

import Demo from '../../app/demo.js';

const { suite, test } = intern.getInterface('tdd');
const { assert } = intern.getPlugin('chai');
const { expect } = intern.getPlugin('chai');
Demo.js类:

class Demo{
  constructor(){

  }

  print(){
    console.log(`hello world`);
  }

}

export default {Demo};
JS的模块系统(导入/导出)还没有得到许多环境的本机支持。要使用这种语法,您需要使用模块加载器,如SystemJS或babel register(参见文档中的)

Intern 4的大多数示例都假设测试是用TypeScript编写的,然后构建到CommonJS或AMD/UMD模块。

JS的模块系统(导入/导出)还没有被许多环境本机支持。要使用这种语法,您需要使用模块加载器,如SystemJS或babel register(参见文档中的)


Intern 4的大多数示例都假设测试是用TypeScript编写的,然后构建到CommonJS或AMD/UMD模块。

如@jason0x43所述,您必须使用加载程序加载ESModule进行测试。 例如,对于babel register,在intern.json文件中,您需要:

"plugins": [
    "node_modules/babel-register/lib/node.js"
]
但这不会加载您的测试文件。因此,如果您的测试文件也是ESModules,那么您可以与结合使用

babel register
已由
intern
加载,因此只需添加一个
.babelrc
文件,其中包含:

{
    "plugins": [
        "transform-es2015-modules-commonjs"
    ]
}

如@jason0x43所述,您必须使用加载程序加载ESModule进行测试。 例如,对于babel register,在intern.json文件中,您需要:

"plugins": [
    "node_modules/babel-register/lib/node.js"
]
但这不会加载您的测试文件。因此,如果您的测试文件也是ESModules,那么您可以与结合使用

babel register
已由
intern
加载,因此只需添加一个
.babelrc
文件,其中包含:

{
    "plugins": [
        "transform-es2015-modules-commonjs"
    ]
}

谢谢,是否可以将Gulp与Babel一起使用并测试捆绑文件?抱歉,单元测试新手,找不到任何测试ES6模块或纯JavaScriptor的示例如果您使用的是像webpack这样的捆绑包,生成的捆绑包将采用与浏览器兼容的语法,您可以将其包含在intern的
套件
列表中。对于babel,您可以使用babel register原样测试代码,或者使用babel to。谢谢,你有一个快速的演示或代码参考,我可以看一下吗?我根据你的建议创建了一个基于()的演示,使用Babel将文件传输到CommonJS。我想知道你是否可以看一看,让我知道我可以如何改进。谢谢,可以使用Gulp和Babel并测试捆绑文件吗?抱歉,单元测试新手,找不到任何测试ES6模块或纯JavaScriptor的示例如果您使用的是像webpack这样的捆绑包,生成的捆绑包将采用与浏览器兼容的语法,您可以将其包含在intern的
套件
列表中。对于babel,您可以使用babel register原样测试代码,或者使用babel to。谢谢,你有一个快速的演示或代码参考,我可以看一下吗?我根据你的建议创建了一个基于()的演示,使用Babel将文件传输到CommonJS。我想知道你是否可以看一看,让我知道如何改进。