-
-
Notifications
You must be signed in to change notification settings - Fork 512
Importing moment in the TypeScript skeleton either breaks the app or the unit tests #606
Comments
Seems to me the tests in the JSPM are somehow set to incorrectly transpile the code, thus failing. I think @bfil is correct about why this currently breaks. Any ideas? |
@niieani Is the test configuration using a different module format perhaps? |
@EisenbergEffect as I pointed out in Gitter, the module specified in https://github.com/systemjs/systemjs/blob/master/lib/legacy-transpiler.js#L80 |
@EisenbergEffect looks like that. In any case, the usage |
Ok, so we need two fixes:
Is that correct? |
Yes, exactly. I've started fixing some docs here: aurelia/binding#453 cause we've had some people complain that code copied from the docs doesn't work: aurelia/binding#452. |
Sorry @niieani but I'm not sure how changing the import helps, if in my
So, how does this help? Here's how my date-format value converter gets transpiled to: (function(System, SystemJS) {define(["require", "exports", 'moment'], function (require, exports, moment_1) {
"use strict";
var DateFormatValueConverter = (function () {
function DateFormatValueConverter() {
}
DateFormatValueConverter.prototype.toView = function (value, format) {
return moment_1.default(value).format(format);
};
return DateFormatValueConverter;
}());
exports.DateFormatValueConverter = DateFormatValueConverter;
});
})(System, System); I've been fighting this for hours so I might be missing something here.. |
The correct way to import it is:
But there's something going wrong with the TS configuration. |
Ok good, then it makes sense. I've been staring at this for too long. I've branched my skeleton-navigation fork if you want to quickly reproduce the issue: https://github.com/bfil/skeleton-navigation/tree/moment-tests-bug A simple |
@bfil see also aurelia/binding#452 |
I tested this in the ESNext skeleton. I have to do |
@AshleyGrant does
|
Although |
Yeah, when I run |
This is all while running in the browser, btw. |
|
Which webpack version are you guys using? There was a bug in Webpack which caused this, but was recently fixed. It works properly for me with the latest skeleton. |
@niieani I'm using the latest version of the Skelton. Just to make sure I cloned a fresh version before posting |
@niieani I just did I just tried it with a fresh copy of the .zip of the skeleton, and first uninstalled my global webpack (1.13.1). I don't think it makes a difference what webpack is installed globally. I am not using the command line webpack with this skeleton. But it's uninstalled now. Now
|
For me
I already deleted node_modules folder and performed a clean |
@guidorice @niieani If I'm not mistake, some of the @easy-webpack configs (like the config-css) use the ExtractTextPlugin |
@mradzinski @guidorice Seems like ExtractTextPlugin has been undergoing some heavy changes in the last 12 hours; I've updated the problematic package to the new syntax. Alternatively you can get around the issue by passing |
FYI: for the time being I've added an horrible workaround to make moment work correctly both in the browser and in the tests:
|
Simpler workaround: no more unit tests! 👅 |
|
Any updates on this? So frustrating that one of the most popular javascript libraries has all these issues running on the skeletons. |
We're looking into migrating to JSPM 0.17, this might be better to fix in that version. |
@EisenbergEffect has this been fixed? if so, any details on what needs to be changed to make this play nicely? Thanks |
For me it worked to install moment-es6 module , declare both (moment and moment-es6) in aurelia.json file, and to link the typings of the library in typings/index.d.ts: reference path="../node_modules/moment/moment.d.ts" |
@EisenbergEffect I am still having troubles to get this working accordingly. |
I'm using the latest version of typescript skeleton. Still having this problem. |
While doing
I was facing the error
So I just added the following to the
which solved my issue. If that could help someone... |
I run into the same situation where I had two files:
with the test throwing the error: When I tried to change both to The solution was: |
Using the Microsoft.AspNetCore.SpaTemplates here, with webpack 2.3.3. |
also broken when using |
Still having the exact same problem, why is this closed ? |
Are you having the problem with JSPM or with Webpack? I can confirm that |
I am not using Aurelia skeleton-navigation, but I used this thread to help troubleshoot when I had the same problem. None of the solutions here worked for me. I fixed the issue by upgrading to typescript >v2.7 and adding Here's a link to the relevant typescript documentation: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#support-for-import-d-from-cjs-form-commonjs-modules-with---esmoduleinterop |
Now we just try to quickly get the date into a yyyy-mm-dd format, and don't mess with it after that. Had to change tsconfig settings and how commonjs modules are imported in order to get both jest and the app to both be happy with moment. See aurelia/skeleton-navigation#606
I'm having this exact problem, but it's the opposite TypeError: flatpickr_1.default is not a function
43 | }
44 | // Setup flatpicker
> 45 | this.flatpickr = flatpickr(this.inputElem, {
| ^
46 | enableTime: false,
47 | altInput: true, If I swap this import to:
Then my Jest tests pass but now flatpickr wont run in the browser. Finally, if I try to override my jest TypeScript config file, this causes "ts-jest" to no longer force the "module" config option to "commonjs". However that just causes the component.viewModel to stop working suddenly. "modulePaths": [
"<rootDir>/src",
"<rootDir>/node_modules"
],
"moduleFileExtensions": [
"js",
"jsx",
"json",
"ts",
"tsx"
],
"cacheDirectory": ".jest/cache",
"transform": {
"^.+\\.tsx?$": "ts-jest",
"^.+\\.jsx?$": "babel-jest"
},
"transformIgnorePatterns": [
"<rootDir>/node_modules/(?!lodash-es)"
],
"testRegex": "\\.spec\\.(ts|js)x?$",
"setupFiles": [
"<rootDir>/test/jest-pretest.ts"
],
"testEnvironment": "node",
"moduleNameMapper": {
"~(.*)$": "<rootDir>/src/$1"
},
"globals": {
"ts-jest": {
"useBabelrc": true,
"tsConfigFile": "tsconfig.jest.json"
}
} tsconfig.jest.json: {
"extends": "./tsconfig",
"compilerOptions": {}
} Output: ● Datepicker › test arrow down opening flatpickr calendar
TypeError: Cannot read property 'flatpickr' of undefined
> 77 | inputElem = component.viewModel.flatpickr._input; I'm going to see if updating ts-jest resolves these problems. |
I was able to get flatpickr and moment working. Needed to change my imports to both look like this across my project: import flatpickr from 'flatpickr';
import moment from 'moment'; tsconfig.json {
"compilerOptions": {
"baseUrl": ".",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es6",
"dom"
],
"module": "esnext",
"moduleResolution": "node",
//
// NOTE: allowSyntheticDefaultImports was required to fix import issues
//
"allowSyntheticDefaultImports": true,
"noUnusedParameters": false,
"noUnusedLocals": false,
"paths": {
"~/*": ["src/*"],
"~": ["src"]
},
"skipLibCheck": true,
"target": "es5",
"typeRoots": [
"node",
"node_modules/@types",
"custom_typings"
]
},
"compileOnSave": true,
"rootDirs": [
"src"
],
"include": [
"src/**/*",
"test/**/*",
"custom_typings/**/*"
],
"exclude": [
"dist",
"node_modules",
"static"
]
} jest-pretest.js import 'aurelia-polyfills';
import { Options } from 'aurelia-loader-nodejs';
import { globalize } from 'aurelia-pal-nodejs';
import * as path from 'path';
Options.relativeToDir = path.join(__dirname, 'unit');
globalize();
// Fix HTMLCollection not working correctly when importing flatpickr + running jest.
global['HTMLCollection'] = window['HTMLCollection'];
// Fix using "new KeyboardEvent" in jest tests.
global['KeyboardEvent'] = window['KeyboardEvent'];
// Add polyfill for "window.requestAnimationFrame" not working correctly running jest.
if (!window['requestAnimationFrame']) {
window['requestAnimationFrame'] = function(callback) {
return window.setTimeout(callback, 0);
};
} Jest config: "modulePaths": [
"<rootDir>/src",
"<rootDir>/node_modules"
],
"moduleFileExtensions": [
"js",
"jsx",
"json",
"ts",
"tsx"
],
"cacheDirectory": ".jest/cache",
"transform": {
"^.+\\.jsx?$": "babel-jest",
"^.+\\.tsx?$": "ts-jest"
},
"transformIgnorePatterns": [
"<rootDir>/node_modules/(?!lodash-es)"
],
"testRegex": "\\.spec\\.(ts|js)x?$",
"setupFiles": [
"<rootDir>/test/jest-pretest.ts"
],
"testEnvironment": "node",
"moduleNameMapper": {
"~(.*)$": "<rootDir>/src/$1"
},
"globals": {
"ts-jest": {
"babelConfig": true
}
} .babelrc {
"env": {
"test": {
"plugins": [
// This is here so that "lodash-es" will transpile when running jest.
// Related issue: https://github.com/kulshekhar/ts-jest/issues/494
"transform-es2015-modules-commonjs",
]
}
}
} |
I was also facing the same problem but below trick solve my problem |
In ma case I fixed the problem by importing moment this way :
instead of |
I am still getting the error TypeError: moment_1.default is not a function I am importing moment as below
tsconfig.json
let me know what i am making a mistake |
I'm submitting a bug report
I'm using moment in my source code, and I usually import it as follows:
import * as moment from 'moment';
This works fine when opening the application in the browser, but when running the tests, the following error is thrown:
moment is not a function
If I change the import to
import moment from 'moment';
then the tests pass, but when opening the application in the browser the following error is thrown:
moment_1.default is not a function
Aurelia Skeleton Version
skeleton-typescript
aurelia-framework version:
1.0.0-rc.1.0.2
Operating System:
OSX 10.x
Node Version:
6.2.0
NPM Version:
3.8.9
JSPM OR Webpack AND Version
JSPM 0.16.32
Browser:
all
Language:
TypeScript 1.8.10 / 2.0.0
Current behavior:
Using
import * as someModule from 'some-module'
, results in different transpiled version of the source code in the tests, which leads to errors being thrown inconsistently.Expected/desired behavior:
It would be ideal if the code build by the gulp tasks would be used by the tests, or at least as similar as possible.
What is the motivation / use case for changing the behavior?
Unable to import global modules like
moment
without either breaking the app or the testsThe text was updated successfully, but these errors were encountered: