微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Tone.js 测试出现 JEST 错误:“ReferenceError: AudioBuffer is not defined”

如何解决Tone.js 测试出现 JEST 错误:“ReferenceError: AudioBuffer is not defined”

我正在使用 Tone.jsTypescript 中创建一个应用程序,我正在使用 Jest 对其进行测试。

我有一个 Musician 类:

import * as Tone from 'tone';

export class Musician {
    instrument: Tone.Synth;

    constructor() {
        this.instrument = new Tone.Synth().toDestination();
    }
}

这就是我测试它的地方:

import { Musician } from '../src/musician';

test("Musician has an instrument",() => {
    const musician = new Musician();

    expect(musician.instrument).tobedefined();
});

当我运行这个测试时,我得到这个:

terminal output

这是我的tsconfig.json

{
  "compilerOptions": {
    "target": "ES5","module": "commonjs","strict": true,"esModuleInterop": true,"skipLibCheck": true,"forceConsistentCasingInFileNames": true
  }
}

这是我的jest.config.js

module.exports = {
  preset: "ts-jest",testEnvironment: "jsdom",};

解决方法

Jest 作为 node.js 脚本运行,而 AudioBuffer 和 Web Audio 通常在 node.js 环境中不可用。

您基本上必须模拟整个 Web Audio API 来测试您的音频内容。但我会看看tone.js库本身,了解他们如何测试它。

https://github.com/Tonejs/Tone.js/blob/dev/test/helper/SourceTests.ts

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。