在 ember mocha 中使用 before all hook 时出现 TypeError

TypeError when using before all hook in ember mocha

我 运行 遇到了 ember-mocha(版本 0.14.0)中 before all 挂钩的一些问题。这是来自 the docs 的一个示例,经过稍微修改以包含 beforeEach 挂钩:

import { expect } from 'chai';
import { describe, it } from 'mocha';
import { setupApplicationTest } from 'ember-mocha';
import { visit, currentURL } from '@ember/test-helpers';

describe('basic acceptance test', function() {
  setupApplicationTest();

  beforeEach(async function() {
    await visit('index');
  });

  it('can visit /', async function() {
    await visit('/');
    expect(currentURL()).to.equal('/');
  });
});

以上测试按预期运行,没有任何问题。但是,当我将 before 替换为 beforeEach 时,我遇到了一个错误:

import { expect } from 'chai';
import { describe, it } from 'mocha';
import { setupApplicationTest } from 'ember-mocha';
import { visit, currentURL } from '@ember/test-helpers';

describe('basic acceptance test', function() {
  setupApplicationTest();

  before(async function() {
    await visit('index');
  });

  it('can visit /', async function() {
    await visit('/');
    expect(currentURL()).to.equal('/');
  });
});
TypeError: Cannot destructure property `owner` of 'undefined' or 'null'.
    at visit (assets/test-support.js:24931:9)
    at Context.<anonymous> (assets/tests.js:339:36)
    at invoke (assets/test-support.js:22801:21)
    at Context.asyncFn (assets/test-support.js:22786:11)
    at callFnAsync (assets/test-support.js:14070:8)
    at Hook.Runnable.run (assets/test-support.js:14022:7)
    at next (assets/test-support.js:14386:10)
    at assets/test-support.js:14408:5
    at timeslice (assets/test-support.js:9651:27)

如果需要任何说明,请告诉我。预先感谢您的帮助!

符合预期! before 所有测试只运行一次。 这意味着在所有 beforeEach 钩子之前 但是 setupApplicationTest 使用 beforeEach 设置应用程序(和容器),并使用 afterEach 再次将其拆除。 这意味着您将获得适用于所有测试的全新应用程序。 但是,如果没有应用程序,您将无法真正访问任何内容。

这意味着对于每个测试,您都会获得一个新的应用程序实例。 这意味着没有适用于所有测试的应用程序,因此没有可以访问路线的应用程序。


在 ember discord 频道中提出了同样的问题。本回答试图将讨论的精髓提取到SO上存档。