nx: Bug(testing): unsupported .js extension

I had checked the new angular 5 with nx workspace. When I run ng test, the following error has occurred:

ERROR in error TS6054: File '.../Angular/mdb-admin/test.js' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.

Any fix on this?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 21 (7 by maintainers)

Commits related to this issue

Most upvoted comments

@Silthus

In the root of your project, rename test.js to test.ts.

Here’s the updated TypeScript version for that file and NX:

// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy.js';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
import { getTestBed } from '@angular/core/testing';
import {
    BrowserDynamicTestingModule,
    platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';

// Unfortunately there's no typing for the `__karma__` variable. Just declare it as any.
declare const __karma__: any;
declare const require: any;

// Prevent Karma from running prematurely.
__karma__.loaded = function () { };

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
    BrowserDynamicTestingModule,
    platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./apps', true, /\.spec\.ts$ /);
// And load the modules.
context.keys().map(context);

const contextLibs = require.context('./libs', true, /\.spec\.ts$/);
// And load the modules.
contextLibs.keys().map(contextLibs);
// Finally, start Karma to run the tests.
__karma__.start();

Update tsconfig.spec.json so that the files section references test.ts instead of test.js.

Lastly, update your .angular-cli.json so that all references of:

"test": "../../../test.js",

are updated to:

"test": "../../../test.ts",

However, we’re using test.ts. If I change the name to test.js so that I don’t have to change the .angular-cli.json every time–you get an error:

If you are moving your existing project into the workspace, by making it an app, you should do without test.ts because it is shared between all the apps. So your existing project and the workspace won’t have the same test.ts.

I strongly believe nrwl should be generating and referencing test.ts since that’s what @angular/cli does by default now.

Agree. We generate test.js because we had an issue with an earlier version of the CLI. The issue might be fixed right now. If you submit a PR changing the application schematic to generate test.ts, I’ll merge it.

@vsavkin Sorry for misunderstood. I thought the generated file should be test.ts, not test.js, and the issue is nx/schematics should generate test.ts like angular/cli. Then when I create a new project and test.js still appears, I thought the issue was not solved.