简体   繁体   English

ES6 / Mocha包含文件

[英]ES6/Mocha Include Files

I'm quite new to ES6 export/import syntax and I would like to know how to dynamically import files with tests inside my indexTest.js file. 我对ES6导出/导入语法非常陌生,我想知道如何在indexTest.js文件中使用测试动态导入文件。

I have 2 files with tests. 我有2个文件进行测试。

PeopleTest.js

/* global it, describe, before, after */
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */

import chai, { expect } from 'chai';
import dirtyChai from 'dirty-chai';

chai.use(dirtyChai);

describe('People tests', () => {
  it('Mock', () => {
    expect(true).to.be.true();
  });
});

PostTest.js

/* global it, describe, before, after */
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */

import chai, { expect } from 'chai';
import dirtyChai from 'dirty-chai';

chai.use(dirtyChai);

describe('Post tests', () => {
  it('Mock', () => {
    expect(true).to.be.true();
  });
});

And I would like to have a global file to import these two files 我想要一个全局文件来导入这两个文件

indexTest.js

/* global it, describe, before, after */
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */

import chai, { expect } from 'chai';
import dirtyChai from 'dirty-chai';

chai.use(dirtyChai);

describe('All tests', () => {
  before(() => {
    // some stuff
  });

  after(() => {
    // some stuff
  });

  import './PeopleTest';
  import './PostTest';
});

But of course its not working because import statement should be at the top level. 但是它当然不起作用,因为import语句应该在顶层。

You could do what CodingIntrigue suggested in a comment and call require instead of using import . 你可以做什么CodingIntrigue在评论建议 ,并调用require的,而不是使用import However, this entails making an assumption regarding the run-time environment, which may not always hold. 但是,这需要对运行时环境进行假设,而这种假设可能并不总是成立。 For instance, if you compile your ES6 code to an environment that uses AMD semantics for loading modules, then the require in your describe will be interpreted as if it were at the top with your import statements, and you won't get the results you want. 例如,如果将ES6代码编译到使用AMD语义加载模块的环境,则describerequire将被解释为好像在import语句中位于顶部,而您将无法获得结果想。

A way to get what you want, that does not assume anything else than the ES6 module loading semantics, would be to just modify the two modules you import so that they export a function that creates the tests they want to run, and then call that function where you need it. 一种获取所需内容的方法,该方法不假设ES6模块加载语义,它只是修改了您导入的两个模块,以便它们导出一个函数来创建要运行的测试,然后调用该模块。在您需要的地方起作用。 This way you decouple module loading from test creation. 这样,您就可以将模块加载与测试创建分离。

One of the modules to be imported could be: 要导入的模块之一可能是:

/* global it, describe, before, after */
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */

import chai, { expect } from 'chai';
import dirtyChai from 'dirty-chai';

export default function peopleTest() {
  chai.use(dirtyChai);

  describe('People tests', () => {
    it('Mock', () => {
      expect(true).to.be.true();
    });
  });
};

And your main test file could become: 您的主要测试文件可能变为:

/* global it, describe, before, after */
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */

import chai, { expect } from 'chai';
import dirtyChai from 'dirty-chai';
import peopleTest from './PeopleTest';
import postTest from './PostTest';

chai.use(dirtyChai);

describe('All tests', () => {
  before(() => {
    // some stuff
  });

  after(() => {
    // some stuff
  });

  peopleTest();
  postTest();    
});

You can only include a script file in an HTML page, not in another script file. 您只能在HTML页面中包含一个脚本文件,而不能在另一个脚本文件中包含。 That said, you can write JavaScript which loads your "included" script into the same page: 也就是说,您可以编写JavaScript来将“包含”脚本加载到同一页面中:

var imported = document.createElement('script');
imported.src = '/path/to/imported/script';
document.head.appendChild(imported);

There's a good chance your code depends on your "included" script, however, in which case it may fail because the browser will load the "imported" script asynchronously. 您的代码很有可能取决于您的“包含”脚本,但是在这种情况下,它可能会失败,因为浏览器将异步加载“导入”脚本。 Your best bet will be to simply use a third-party library like jQuery or YUI, which solves this problem for you. 最好的选择是仅使用jQuery或YUI之类的第三方库,从而为您解决此问题。

// jQuery
$.getScript('/path/to/imported/script.js', function()
{
    // script is now loaded and executed.
    // put your dependent JS here.
});

src: from this question src: 从这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM