Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not find module qunit imported from <my-app or addon>/tests/test-helper #436

Closed
NullVoxPopuli opened this issue Aug 17, 2021 · 13 comments

Comments

@NullVoxPopuli
Copy link
Contributor

NullVoxPopuli commented Aug 17, 2021

Uncaught Error: Could not find module `qunit` imported from `<my-app>/tests/test-helper`
    missingModule http://localhost:4200/<my-app>/assets/vendor.js:6403
    findModule http://localhost:4200/<my-app>/assets/vendor.js:6414
    findDeps http://localhost:4200/<my-app>/assets/vendor.js:6324
    findModule http://localhost:4200/<my-app>/assets/vendor.js:6418
    requireModule http://localhost:4200/<my-app>/assets/vendor.js:6180
    <anonymous> http://localhost:4200/<my-app>/assets/tests.js:1445

This is a runtime error that only occurs when visiting /tests (and running ember test)

Using: ember-auto-import@2.1.0 (with a bunch of addons using 1.x)

yarn list ember-auto-import
❯ yarn list ember-auto-import
├─ ember-auto-import@2.1.0     <--------------- this is the one specified in my app
├─ ember-changeset@3.14.1
│  └─ ember-auto-import@1.11.3
├─ ember-exam@6.1.0
│  └─ ember-auto-import@1.11.3
├─ ember-focus-trap@0.7.0
│  └─ ember-auto-import@1.11.3
├─ ember-highcharts@3.0.1
│  └─ ember-auto-import@1.11.3
├─ ember-intl@5.7.0
│  └─ ember-auto-import@1.11.3
├─ ember-qunit@5.1.4
│  └─ ember-auto-import@1.11.3
├─ ember-statechart-component@2.3.6
│  └─ ember-auto-import@1.11.3
├─ ember-statecharts@0.13.2
│  └─ ember-auto-import@1.11.3
├─ ember-useragent@0.10.0
│  └─ ember-auto-import@1.11.3
Done in 3.66s.

config in ember-cli-build:

babel: {
  plugins: [
    require.resolve('ember-auto-import/babel-plugin'),
  ],
},
'ember-cli-babel': {
  enableTypeScriptTransform: true,
},
autoImport: {
  publicAssetUrl: '...',
  skipBabel: [],
  
}
@ef4
Copy link
Collaborator

ef4 commented Aug 18, 2021

Is tests/test-helper authored in typescript?

(Just like in embroider-build/embroider#889, you may be making life unnecessarily hard for yourself by trying to use typescript without using ember-cli-typescript. That is not a well-trodden path.)

Please add a console.log or breakpoint here:

https://github.com/ef4/ember-auto-import/blob/203ab5886abe93996ed2e2363b0f2d9ee9f8997c/packages/ember-auto-import/ts/package.ts#L165

to see what is in extensions. If it doesn't include TS, that's the problem, and the issue may be upstream in ember-cli-babel, assuming the way you're configuring ember-cli-babel is supported, because we get the extensions list from ember-cli-babel.

This reminds me vaguely of emberjs/ember-cli-babel#401 also but I don't know if the exact conditions are the same.

@ef4
Copy link
Collaborator

ef4 commented Aug 18, 2021

As a workaround you could try:

'ember-cli-babel': {
  enableTypeScriptTransform: true,
+ extensions: ['js', 'ts']
},

@NullVoxPopuli
Copy link
Contributor Author

Is tests/test-helper authored in typescript?

it is! how'd you know? ;)

you may be making life unnecessarily hard for yourself by trying to use typescript

pre, ember-auto-import/embroider, ember-cli-typescript provided negative value for me/my work because we need to have composite: true TS projects with references so that we can reference types in other packages within our monorepo without building each of those projects first. As the number of our projects grows, we may require that we pre-build everything, but that has its own set of problems with how often code is updated across the monorepo.

That is not a well-trodden path.

well, it's our only option.

That said, since both ember-auto-import and embroider switch behaviors based off the presence of ember-cli-typescript in package.json, I've tried adding it to my projects (and changing nothing else about our tsconfigs, etc), and it did not solve the problem. In an addon, I still get the same error about qunit missing. (ember-cli-typescript is in dependencies).

As a workaround you could try:

this had no effect, error persists.

Please add a console.log or breakpoint here:

I went the console.log route, and it seems it prints multiple times

addon: console.log(instance.name, extensions, 'version', version);
ember-intl [ 'js', 'ts' ] version 7
<the addon that I'm getting this qunit error on> [ 'js', 'ts' ] version 7
ember-exam [ 'js' ] version 7
ember-qunit [ 'js' ] version 7
ember-useragent [ 'js' ] version 7
<another addon of ours, but this one has no ts> [ 'js' ] version 7
[Function: name] [ 'js', 'ts' ] version 7

I was curious about the extensions setting in the TS projects where I haven't setup ember-cli-typescript (all but one now (maybe ~40))),
It looks like every internal addon we have that is just setting enableTypeScriptTransform: true is also getting the ts extension added to extensions.

app: console.log(instance.name, extensions, 'version', version);
IS_DEVELOPING_ADDON=true DISABLE_SOURCEMAPS=true DISABLE_FINGERPRINTING=true DISABLE_UGLIFY=true ember serve --watcher polling --ssl false

⠏ building... [Babel: ember-browser-services > applyPatches]

ember-intl [ 'js', 'ts' ] version 7
<Addon A (the one ran above)> [ 'js', 'ts' ] version 7
<Addon B> [ 'js', 'ts' ] version 7
<Addon C> [ 'js', 'ts' ] version 7
<Addon D>  [ 'js', 'ts' ] version 7
ember-animated [ 'js', 'ts' ] version 7
<Addon E>  [ 'js', 'ts' ] version 7
<Addon F> t [ 'js', 'ts' ] version 7
<Addon G>  [ 'js', 'ts' ] version 7
ember-qunit [ 'js' ] version 7
ember-useragent [ 'js' ] version 7
<Addon A> [ 'js', 'ts' ] version 7
ember-changeset [ 'js' ] version 7
ember-focus-trap [ 'js' ] version 7
ember-statechart-component [ 'js', 'ts' ] version 7
<Addon B>  [ 'js', 'ts' ] version 7
<Addon E>  [ 'js', 'ts' ] version 7
<Addon H> [ 'js' ] version 7 <-- no TS, so this is good
<Addon A> [ 'js', 'ts' ] version 7
<Addon i>  [ 'js', 'ts' ] version 7
[Function: name] ext [ 'js', 'ts' ] version 7      <- because this is last, I assume this is the app?

what else could it be? the extensions from this list seem to not be the issue (or whatever is looking for test-helper.js/ts isn't using these extensions?

@ef4
Copy link
Collaborator

ef4 commented Aug 18, 2021

Maybe TS is not the issue then. The relevant bit is probably not this:

<the addon that I'm getting this qunit error on> [ 'js', 'ts' ] version 7

But this:

[Function: name] [ 'js', 'ts' ] version 7

Because that is probably the dummy app (apps and addons in ember-cli have a different type for this.name, sigh). It's the dummy app that needs TS support to get tests/test-helper.ts working, and it seems to have it.

Check that the problematic package has followed the ember-qunit migration guide for 5.x. https://github.com/emberjs/ember-qunit/blob/master/docs/migration.md#upgrading-from-v4x-to-v500

By running with DEBUG=ember-auto-import:* and searching for tests/test-helper and qunit you should be able to see whether the file is being analyzed, what imports we found in it, and what we decided we needed to auto import.

@NullVoxPopuli
Copy link
Contributor Author

has followed the ember-qunit migration guide for 5.x

it has 👍

By running with DEBUG=ember-auto-import:*

I had to pipe all this out to a file, because it far surpassed by terminal's buffer history (only 10k lines),
so I ran: AUTO_IMPORT_VERBOSE=true DEBUG="ember-auto-import:*" ember build 2> error.txt 1> output.txt

results for tests/test-helper
2021-08-18T14:52:33.200Z ember-auto-import:analyzer updating imports for dummy/tests/test-helper.ts, 1401

/// ...

2021-08-18T14:52:48.797Z ember-auto-import:analyzer imports [

  {
    "specifier": "dummy/app",
    "path": "dummy/tests/test-helper.ts",
    "isDynamic": false,
    "package": "<my-addon>",
    "treeType": "test"
  },
  {
    "specifier": "dummy/config/environment",
    "path": "dummy/tests/test-helper.ts",
    "isDynamic": false,
    "package": "<my-addon>",
    "treeType": "test"
  },
  {
    "specifier": "qunit",
    "path": "dummy/tests/test-helper.ts",
    "isDynamic": false,
    "package": "<my-addon>",
    "treeType": "test"
  },
  {
    "specifier": "@ember/test-helpers",
    "path": "dummy/tests/test-helper.ts",
    "isDynamic": false,
    "package": "<my-addon>",
    "treeType": "test"
  },
  {
    "specifier": "@ember/test-waiters",
    "path": "dummy/tests/test-helper.ts",
    "isDynamic": false,
    "package": "<my-addon>",
    "treeType": "test"
  },
  {
    "specifier": "ember-sinon-qunit",
    "path": "dummy/tests/test-helper.ts",
    "isDynamic": false,
    "package": "<my-addon>",
    "treeType": "test"
  },
  {
    "specifier": "sinon",
    "path": "dummy/tests/test-helper.ts",
    "isDynamic": false,
    "package": "<my-addon>",
    "treeType": "test"
  },
  {
    "specifier": "ember-exam/test-support/start",
    "path": "dummy/tests/test-helper.ts",
    "isDynamic": false,
    "package": "<my-addon>",
    "treeType": "test"
  },
  {
    "specifier": "qunit-dom",
    "path": "dummy/tests/test-helper.ts",
    "isDynamic": false,
    "package": "<my-addon>",
    "treeType": "test"
  },
  {
    "specifier": "<my-addon>/test-support/faker",
    "path": "dummy/tests/test-helper.ts",
    "isDynamic": false,
    "package": "<my-addon>",
    "treeType": "test"
  },
  {
    "specifier": "<my-addon>/test-support/<some-file>",
    "path": "dummy/tests/test-helper.ts",
    "isDynamic": false,
    "package": "<my-addon>",
    "treeType": "test"
  },
  
  /// ...
  2021-08-18T14:52:48.928Z ember-auto-import:splitter bundleForPath("dummy/tests/test-helper.ts")=tests
  
  /// ...
    "tests": {
    "static": [
       /// ...
        {
        "specifier": "qunit",
        "packageRoot": "<snip>/node_modules/qunit",
        "importedBy": [
           /// ...
            {
            "package": "<my-addon>",
            "path": "dummy/tests/test-helper.ts"
          },


So, it seems like it's being analyzed 🤔
Searching for qunit shows a lot of results, so I'm not sure what to look for.
With this kind of output, how do you know if something is going to be auto-imported?

@ef4
Copy link
Collaborator

ef4 commented Aug 18, 2021

Those logs show that the qunit import was found and correctly categorized as going in the tests bundle.

Perhaps your tests bundle isn't being loaded at all? Run this (much smaller) log dump: DEBUG=embroider:inserter

Does your tests/index.html do anything nonstandard to include test-support.js? ember-auto-import places the chunks for the tests bundle immediately after test-support.js. If we fail to find it, you won't have them.

@NullVoxPopuli
Copy link
Contributor Author

NullVoxPopuli commented Aug 18, 2021

log dump: DEBUG=embroider:inserter

This didn't change any output -- is this an embroider-only DEBUG flag?

out of curiosity / guessing at the debug flag conventions, I tried:
DEBUG=ember-auto-import:inserter ember build and it had output 🎉

DEBUG=ember-auto-import:inserter ember build
 ember-auto-import:inserter parsing index.html +0ms
  ember-auto-import:inserter looking for scripts with src: [ 'assets/vendor.js', 'assets/test-support.js' ] +22ms
  ember-auto-import:inserter looking for link with href: [] +0ms
  ember-auto-import:inserter found stylesheet with href=/assets/vendor.css +1ms
  ember-auto-import:inserter found stylesheet with href=/assets/dummy.css +0ms
  ember-auto-import:inserter found script with src=/assets/vendor.js +0ms
  ember-auto-import:inserter inserting [
  'assets/chunk.vendors-node_modules_fingerprintjs_fingerprintjs_dist_fp_esm_js-node_modules_common-tags_es_i-fd8ec1.74a85e009bd5c61728eb.js',
  'assets/chunk.app.34c1ebaed3b3c788b75f.js'
] +0ms
  ember-auto-import:inserter found script with src=/<my-addon>/fixtures.js +0ms
  ember-auto-import:inserter found script with src=/assets/dummy.js +1ms
  ember-auto-import:inserter parsing tests/index.html +0ms
  ember-auto-import:inserter looking for scripts with src: [ 'assets/vendor.js', 'assets/test-support.js' ] +16ms
  ember-auto-import:inserter looking for link with href: [] +1ms
  ember-auto-import:inserter found stylesheet with href=/assets/vendor.css +0ms
  ember-auto-import:inserter found stylesheet with href=/assets/dummy.css +0ms
  ember-auto-import:inserter found stylesheet with href=/assets/test-support.css +0ms
  ember-auto-import:inserter found script with src=/testem.js +0ms
  ember-auto-import:inserter found script with src=/assets/vendor.js +0ms
  ember-auto-import:inserter inserting [
  'assets/chunk.vendors-node_modules_fingerprintjs_fingerprintjs_dist_fp_esm_js-node_modules_common-tags_es_i-fd8ec1.74a85e009bd5c61728eb.js',
  'assets/chunk.app.34c1ebaed3b3c788b75f.js'
] +0ms
  ember-auto-import:inserter found script with src=/assets/test-support.js +0ms
  ember-auto-import:inserter inserting [
  'assets/chunk.vendors-node_modules_lodash_castarray_index_js-node_modules_lodash_last_index_js-node_modules-f622ad.5ac2129b73db718991aa.js',
  'assets/chunk.tests.a9911cadb9dabdd8979c.js'
] +0ms
  ember-auto-import:inserter found script with src=/assets/dummy.js +0ms
  ember-auto-import:inserter found script with src=/assets/tests.js +0ms

I see a couple references to test-support.js 🤷
the test-support.js file exists in dist/assets, as I'd expect. 🤔

maybe related, maybe not, this addon has a number of this.imports in the index.js, but.. that should be fine? (they all use the amd transformation)

Does your tests/index.html do anything nonstandard

I don't believe so? this looks identical(?) to a new addon's tests/index.html. (3.28.0-alpha.1-switch-away-from-travis-04a91e4ab6 (I was surprised at this version, so I re-volta install ember-cli and am now back on 3.27.0 🙈 -- tests/index.html still matches))

tests/index.html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Dummy Tests</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    {{content-for "head"}}
    {{content-for "test-head"}}

    <link rel="stylesheet" href="{{rootURL}}assets/vendor.css">
    <link rel="stylesheet" href="{{rootURL}}assets/dummy.css">
    <link rel="stylesheet" href="{{rootURL}}assets/test-support.css">

    {{content-for "head-footer"}}
    {{content-for "test-head-footer"}}
  </head>
  <body>
    {{content-for "body"}}
    {{content-for "test-body"}}

    <div id="qunit"></div>
    <div id="qunit-fixture">
      <div id="ember-testing-container">
        <div id="ember-testing"></div>
      </div>
    </div>

    <script src="/testem.js" integrity="" data-embroider-ignore></script>
    <script src="{{rootURL}}assets/vendor.js"></script>
    <script src="{{rootURL}}assets/test-support.js"></script>
    <script src="{{rootURL}}assets/dummy.js"></script>
    <script src="{{rootURL}}assets/tests.js"></script>

    {{content-for "body-footer"}}
    {{content-for "test-body-footer"}}
  </body>
</html>

@NullVoxPopuli
Copy link
Contributor Author

NullVoxPopuli commented Aug 18, 2021

I searched for tests/test-helper in my dist/assets -- it looks like it is included in tests.js .
searching for qunit, shows nothing in dist/assets 🤔

# in dist/assets
ag "define\(\"qunit"
# -> one result in test-support for qunit-dom/index
ag "define\(\"dummy/tests/test-helper"
# -> one result in tests.js

I now understand what you meant by:

Those logs show that the qunit import was found and correctly categorized as going in the tests bundle.

@NullVoxPopuli NullVoxPopuli changed the title Could not find module qunit imported from <my-app>/tests/test-helper Could not find module qunit imported from <my-app or addon>/tests/test-helper Aug 18, 2021
@NullVoxPopuli
Copy link
Contributor Author

could this have to do with qunit being in both devDeps and peerDeps? 🤔

@NullVoxPopuli
Copy link
Contributor Author

nope, that's not it. I removed qunit from devDeps and peerDeps, and placed it in deps, and nothing changed 🤔

@NullVoxPopuli
Copy link
Contributor Author

I'm going to close this for now, as I think something "wonky" is going on. In isolation the right thing to do is put qunit in deps or peerdeps, and that works fine.

@kas1337
Copy link

kas1337 commented Feb 15, 2022

So, I had the same issue. Here's my journey:

For a very long time I would stay on ember-auto-import@2.0.2 because any newer version would add a wrong path fpr the chunk.js files in my index.html produced by the production build.

It would add

<script src="/assets/chunk[...]" ></script>

instead of

<script src="assets/chunk[...]"></script>

Notice the extra slash in the upper snippet. This would lead to a 404 error when my app was deployed on a subsite e.g. under https://myapp.com/v1 instead of the main site https://myapp.com/ because the browser would try to load the chunk.js file from https://myapp.com/.
ember test would run fine though.

Since no one else seemed to have this issue I decided to try out some options. Setting

autoImport: {
            publicAssetURL: 'assets/'
}

in ember-cli-build.js fixed my issue. The path for the chunks.js files was correct again. But the tests wouldn't run anymore! They kept failing with the same error you described here.

So I changed the snippet in ember-cli-build.js to

autoImport: {
            publicAssetURL: IS_TEST ? "/assets/" : 'assets/'
}

with IS_TEST just being a variable in the same file that checks if the environment is test

const EmberApp = require("ember-cli/lib/broccoli/ember-app");

const environment = EmberApp.env();
const IS_TEST = (environment === "test");

Now everything works like a charm (so far). Unfortunately I don't quite understand what's going on here.

I hope this might help some people and maybe someone can dig in further.

Btw, qunit is in my devDeps.

@ef4
Copy link
Collaborator

ef4 commented Feb 15, 2022

@kas1337 that makes sense.

I think you need to set rootURL (in config/environment.js) correctly. If <script src="assets/whatever"> works but <script src="/assests/whatever"> doesn't it's because your ember app is running under a path that's not "/" so you need to set rootURL to the true path where the app runs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants