本文介绍了节点-将笑话与esm软件包一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何整合esm https://www.npmjs.com/包/esm ,在节点后端带有笑话.

我尝试使用文件顶部的require("esm")require("esm")(module)来设置安装文件,但是仍然出现SyntaxError: Unexpected token错误.

我以前会使用node -r esm,但开玩笑不支持此操作.

解决方案

执行require("esm")(module)时,请在创建esm-transformer函数时将其想到,该函数正在等待文件转换为ES模块. /p>

这是我尝试使用以下节点v8 +:

  • 默认jest配置

  • 默认esm配置

utils-1.js:

export const add = (a, b) => a + b;

utils-2.js:

export const multiAdd = array => array.reduce((sum, next) => sum + next, 0)

_test_/utils-1.assert.js

import { add } from '../utils-1';

describe('add(a,b)', () => {
  it('should return the addtion of its two inputs', () => {
    expect(add(1,2)).toBe(3);
  });
});

_test_/utils-2.assert.js

import { multiAdd } from '../utils-2';

describe('multiAdd(<Number[]>)', () => {
  it('should return a summation of all array elements', () => {
    expect(multiAdd([1,2,3,4])).toBe(10);
  })
});

_test_/utils.test.js

const esmImport = require('esm')(module);
const utils_1 = esmImport('./utils-1.assert')
const utils_2 = esmImport('./utils-2.assert')

希望这会有所帮助!

I was wondering how I would incorporate the esm package https://www.npmjs.com/package/esm with jest on a node backend.

I tried setting up a setup file with require("esm") and require("esm")(module) at the very top of the file, but it's still giving me the SyntaxError: Unexpected token error.

I would have previously used node -r esm but jest doesn't support this.

解决方案

When you perform require("esm")(module), think of it as you are creating an esm-transformer function that is pending a file to be transformed into an ES module.

Here's my attempt with node v8+ with:

  • default jest configuration

  • default esm configuration

utils-1.js:

export const add = (a, b) => a + b;

utils-2.js:

export const multiAdd = array => array.reduce((sum, next) => sum + next, 0)

_test_/utils-1.assert.js

import { add } from '../utils-1';

describe('add(a,b)', () => {
  it('should return the addtion of its two inputs', () => {
    expect(add(1,2)).toBe(3);
  });
});

_test_/utils-2.assert.js

import { multiAdd } from '../utils-2';

describe('multiAdd(<Number[]>)', () => {
  it('should return a summation of all array elements', () => {
    expect(multiAdd([1,2,3,4])).toBe(10);
  })
});

_test_/utils.test.js

const esmImport = require('esm')(module);
const utils_1 = esmImport('./utils-1.assert')
const utils_2 = esmImport('./utils-2.assert')

Hope this helps!

这篇关于节点-将笑话与esm软件包一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 19:23