将一个 js 文件包含在另一个文件中的正确方法是什么?

What is the correct way to include one js file in another?

以下Java脚本代码有效:

let Color = require('color');

class Example {
    its_not_easy_being_green() {
        return Color('green');
    }
}

test('It is not easy being green', () => {
    expect(new Example().its_not_easy_being_green()).toBeDefined();
})

npx jest给我一个绿条。

$ npx jest
 PASS  test/example.spec.js
  ✓ It is not easy being green (2 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.351 s, estimated 1 s
Ran all test suites.

但是,每当我尝试将 class 与测试分开时,我都会遇到各种错误:

最常见的是不允许在模块外使用导入。

或者 Example 不是构造函数。

我想做的是将我在上面发布的 Java 脚本文件的三行测试 之外的所有内容移出

如果这是 Java,我会把这些东西移到一个单独的 .java 文件中,然后 import 在我的测试中那个文件。

如果这是 C 语言,就会有一个链接器并且 #include 涉及。

我尝试创建一个 Example.js 文件:

let Color = require('color');

class Example {
    its_not_easy_being_green() {
        return Color('green');
    }
}

然后require它在测试中:

let Example = require('Example.js')

test('It is not easy being green', () => {
    expect(new Example().its_not_easy_being_green()).toBeDefined();
})

但这让我抱怨 Example 不是构造函数,我想这是真的。它是一个 class,大概有某种非常惰性的默认构造函数。

我也试过用 import 替换 require:

import('Example.js')

test('It is not easy being green', () => {
    expect(new Example().its_not_easy_being_green()).toBeDefined();
})

但是有人抱怨不允许在模块外使用 import

是否可以在 JavaScript 中将玩笑测试与正在测试的代码分开?

Javascript 中没有 #include - 它使用模块系统代替 export 您想要分享的东西。

最好学习该语言提供的工具,而不是试图强迫它按照其他语言的方式做某事。

此外,请注意 nodejs 中有两种类型的模块,使用 require()module.exports 的 CommonJS 模块以及使用 importexport 的 ESM 模块。您的项目似乎是为 CommonJS 设置的,所以这就是您要使用的项目,除非您想将项目配置为 ESM 模块项目,然后使用 ESM 规则进行导入和导出。

对于 CommonJS 项目:

const Color = require('color');

class Example {
    its_not_easy_being_green() {
        return Color('green');
    }
}

module.exports.Example = Example;

然后,在你的主文件中:

const { Example } = require('./Example.js')

test('It is not easy being green', () => {
    expect(new Example().its_not_easy_being_green()).toBeDefined();
});

仅供参考,require(someModule) 可以得到 module.exports 的任何内容。如果你直接赋值给 module.exports 比如:

// in color.js

module.exports = function Color() {
     // do something here
}

然后,你会像这样使用它:

const Color = require('color');

但是,如果您只是将 属性 添加到 module.exports 对象,例如:

// in Example.js
const Color = require('color');

class Example {
    its_not_easy_being_green() {
        return Color('green');
    }
}

module.exports.Example = Example;

那么您必须通过以下任一方式从导出的对象中获取 .Example 属性:

const { Example } = require('./Example.js');

const Example = require('./Example.js').Example;

const exampleExports = require('./Example.js');
const Example = exampleExports.Example;

注意,语法中的括号:

const { Example } = require('./Example.js');

称为“对象解构”,是一种快捷语法。

上面的 color.js 方法是在模块只有一个主要导出时完成的,因此您可以直接导出函数而不是导出对象。

上面的 example.js 方法是在你从模块中有多个主导出时完成的(或者你想保留在将来有多个 top-level 导出的灵活性),比如这个:

// in Example.js
const Color = require('color');

class Example {
    its_not_easy_being_green() {
        return Color('green');
    }
}

class SubExample extends Example {
    its_not_easy_being_red() {
        return Color('red');
    }
} 

// multiple exports
module.exports.Example = Example;
module.exports.SubExample = SubExample;