I am running ava with ts-node with the following config:
"ava": {
"files": [
"tests/**/*",
"!test/exclude-files-in-this-directory",
"!**/exclude-files-with-this-name.*"
],
"failFast": true,
"failWithoutAssertions": false,
"extensions": [
"spec.ts",
"ts"
],
"environmentVariables": {
"NODE_ENV": "test"
},
"require": [
"ts-node/register",
"tsconfig-paths/register"
],
"tap": false,
"verbose": true
}
The problem is that .spec.ts files don't get recognized, as ts-node is doing some kind of conversion (I think) of the .spec.ts files into just .ts, which means that the extension with spec.ts is never matched.
Here's the output of the tests
- graphql › stage-2 › workspace.ts › do later
✔ general › functions.ts › returns proper difference
The files are named workspace.spec.ts and functions.spec.ts
Is there anyway for ts-node to not drop the spec part?
ts-node should only kick in when AVA requires the test file. I can't immediately spot why this isn't working. Are those spec.ts files inside the tests directory?
P.S. NODE_ENV already defaults to test, and you don't need to disable tap either.
Related
I have stylelint installed in my project, and I've configured its configuration.
I added a script to run this linter on my src folder.
For some reason, the linter scans only one folder.
Here is my configuration file stylelint.config.js:
module.exports = {
extends: [
'stylelint-config-standard-scss',
'stylelint-config-prettier-scss',
'stylelint-config-recess-order',
],
plugins: ['stylelint-scss', 'stylelint-order'],
rules: {
'selector-class-pattern': [
'^[a-z][A-Za-z0-9]*((--([a-z][A-Za-z0-9]*)(__([a-z][A-Za-z0-9]*))?)|(__([a-z][A-Za-z0-9]*)(--([a-z][A-Za-z0-9]*))?))?$',
{ resolveNestedSelectors: true, message: 'Expected class selector to be camel case' },
],
'value-no-vendor-prefix': null,
'selector-id-pattern': null,
'scss/at-import-partial-extension': null,
},
};
This is the script: "stylelint": "stylelint --f verbose src/**/*.scss",
My src folder has a lot of .scss files. But this script only scans 2 files for some reason.
$ stylelint --f verbose src/**/*.scss
2 sources checked
/Users/amir/Desktop/Development/Vinyl projects/LandingPag-REAL/src/styles/custom.scss
/Users/amir/Desktop/Development/Vinyl projects/LandingPag-REAL/src/styles/variables.scss
0 problems found
✨ Done in 0.79s.
Why would it ignores all other files? I don't have some "ignore" configuration file.
NOTE: It worked on Windows perfect (didn't skip), on Mac it skips almost the entire src file
Also when I change the script to run stylelint ... **/*.scss it does work
You need to quote your input glob, otherwise the shell (which differs on Windows and Mac) will interpret it rather than Stylelint itself.
If you're only targeting *nix, you can use single quotes:
"stylelint": "stylelint --f verbose 'src/**/*.scss'",
For cross-platform use escaped double quotes:
"stylelint": "stylelint --f verbose \"src/**/*.scss\"",
Incidentally, you:
can remove the plugins property as both plugins are bundled in their respective configs
should put the prettier config last so that it overrides everything before
{
"extends": [
"stylelint-config-standard-scss",
"stylelint-config-recess-order",
"stylelint-config-prettier-scss"
],
"rules": {
"selector-class-pattern": [
"^[a-z][A-Za-z0-9]*((--([a-z][A-Za-z0-9]*)(__([a-z][A-Za-z0-9]*))?)|(__([a-z][A-Za-z0-9]*)(--([a-z][A-Za-z0-9]*))?))?$",
{
"resolveNestedSelectors": true,
"message": "Expected class selector to be camel case"
}
],
"value-no-vendor-prefix": null,
"selector-id-pattern": null,
"scss/at-import-partial-extension": null
}
}
I'm attempting make a few functions using the Test Driven Development (TDD)
I am writing in javascript.
checkTransparency(urlString)
maketransparent(urlString)
are two functions of mine I'm trying to test and develop which is located in a file called transcript.js.
These uses the inkscape and graphicsmagick npm. I checked checkTransparent works in some other project of mine, but I'm trying to make sure I can just copy paste this transparent.js into another project and use it elsewhere as well.
My folder structure of the project are the following :
+ node_modules
+ src
--- transparent.js
+ test
--- transparent.spec.js
+ package.json
+ package-lock.json
+ jest.config.js
I am using jest as my test framework.
The problem is when I run jest (or npm test)
I get the following:
FAIL test/transparent.spec.js
● Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
\\..............\transparent\test\transparent.spec.js:4 <FEW DETAILS OMITTED HERE DELIBERATELY>
import { checkTransparency, makeTransparent } from "../src/transparent"; // const transparent = require("../src/transparent");
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime._execModule (C:/Users/Kjeong/AppData/Local/Yarn/Data/global/node_modules/jest-runtime/build/index.js:988:58)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.862s
Ran all test suites.
my jest.config.js:
module.exports = {
testEnvironment: "node",
moduleDirectories: ["node_modules", "src", "transparent"],
moduleFileExtensions: [
"js",
"json",
"jsx",
"ts",
"tsx",
"node"
],
clearMocks: true,
}
I've tried the following exports to get this thing working:
export function checkTransparency(urlString) { ... }
export function makeTransparent(urlString) {... }
module.exports = {
checkTransparency: checkTransparency,
makeTransparent: makeTransparent,
};
In your package.json, using configuration like following could solve your problem:
{
"name": "<blah blah>",
"version": "1.0.0",
"description": "",
"type": "module",
"scripts": {
"start": "node server.js",
"test": "node --experimental-vm-modules node_modules/.bin/jest"
},
}
If you really want to use import keyword then you probably need to follow these explanations. Otherwise why not just require ?
const { checkTransparency, makeTransparent } = require('../src/transparent')
Hope this helps :)
Just created a new TypeScript Aurelia project using aurelia-cli .
Installed bootstrap and included bootstrap css in the app.ts using import.
import 'bootstrap/dist/css/bootstrap.min.css';
import '../../static/assets/css/app.scss';
import { routes } from './routes';
interface IApp {
message: string;
}
export class App implements IApp{
message = 'Hello World!';
}
Now when I run the test , I get error unexpected token as below
yarn test
# and the output contains
yarn run v1.12.3
$ nps test
nps is executing `test` : nps test.jest
nps is executing `test.jest` : node node_modules/rimraf/bin.js test/coverage-jest && jest
ts-jest[config] (WARN) TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
message TS151001: If you have issues related to imports, you should consider setting `esModuleInterop` to `true` in your TypeScript configuration file (usually `tsconfig.json`). See https://blogs.msdn.microsoft.com/typescript/2018/01/31/announcing-typescript-2-7/#easier-ecmascript-module-interoperability for more information.
FAIL test/unit/app.spec.ts
● Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To hav`enter code here`e some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
SyntaxError: Unexpected token :
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
at Object.<anonymous> (src/home/app.ts:163:1)
I commented the import bootstrap.css line in app.ts and everything runs fine.
Am I missing some configuration for jest to allow me to use css imports in .ts components?
Here is my jest portion from package.json
"jest": {
"verbose": true,
"roots": [
"<rootDir>/test"
],
"modulePaths": [
"<rootDir>/src",
"<rootDir>/node_modules"
],
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"transform": {
"^.+\\.ts$": "ts-jest"
},
"testRegex": "\\.spec\\.(ts|js)$",
"testPathIgnorePatterns": [
"build",
"dist",
"sample"
],
"setupFiles": [
"<rootDir>/test/jest-pretest.ts"
],
"testEnvironment": "node",
"collectCoverage": true,
"collectCoverageFrom": [
"src/**/*.{js,ts}",
"!**/*.spec.{js,ts}",
"!**/node_modules/**",
"!**/test/**"
],
"coverageDirectory": "<rootDir>/test/coverage-jest",
"coverageReporters": [
"json",
"lcov",
"text",
"html",
"clover"
]
},
I was getting the same error. I resolved my issue by changing the module compilerOption in tsconfig.json from "esnext" to "commonjs". Why am I getting “Unexpected token import” on one webpack project but not the other?
I have a simple javascript project that is using ES6, plus import and export. Here is my .babelrc file
{
"env": {
"es": {
"presets": [
[
"env",
{
"targets": {
"browsers": ["last 2 versions"],
"node": "current"
},
"modules": false
}
]
],
"ignore": ["**/*.test.js", "**/tests/*"]
},
"test": {
"presets": ["env"]
},
"cjs": {
"presets": [
[
"env",
{
"targets": {
"browsers": ["last 2 versions"],
"node": "current"
}
}
]
],
"ignore": ["**/*.test.js", "**/tests/*"]
}
}
}
I run my tests with npm test, which runs this command out of my package.json: "test": "jest --notify".
Locally, everything runs fine and all my tests pass, but when I push to Travis, I get this error: SyntaxError: Unexpected token import. It does not recognize my import statements.
Here is my .travis.yml:
language: node_js
cache:
yarn: true
node_js:
- '8.4'
script:
- npm test
env:
- NODE_ENV: feature/travis
Why is my .babelrc not working on Travis? What am I missing?
This is likely happening because you have defined NODE_ENV: feature/travis in your travis.yml. This will cause Travis to run your tests as NODE_ENV=feature/travis jest --notify.
This generally shouldn't cause concern, but jest will automatically set the NODE_ENV to test if it is not already set.
It could be that you're issue is similar to the discussion taking place over at https://github.com/facebook/jest/issues/3370. There is some contention on whether Jest should be automatically setting the NODE_ENV to test or if it should just use development if no NODE_ENV is present (or just respect whatever NODE_ENV is already set if its set globally).
You can also see this logic in the jest-cli https://github.com/facebook/jest/blob/master/packages/jest-cli/bin/jest.js#L12-L14
So my recommendation is to test this without having your NODE_ENV set in your travis.yml to see if it successfully runs all of your tests.
I need to setup jest and JavaScript Standard Style to work together when using npm test.
Now when I am running npm test the test fails because JavaScript Standard Style thrown an errors:
'test' is not defined.
'expect' is not defined.
I can work around this issue by defining in my package.json file some global for jest.
"standard": {
"globals": [
"fetch",
"test",
"expect"
]
}
But definitely I do not think it is a good solution.
In my test case sum.test.js
const sum = require('./sum')
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3)
})
In my package.json :
"scripts": {
"test": "standard && jest",
}
Question:
How to configure JavaScript Standard Style so it does not thrown an error when used with jest?
I was able to find a solution.
In package.json
"standard": {
"env": [ "jest" ]
}
Or in the test case:
/* eslint-env mocha */
In your .eslintrc.js file, include the following setting and you'll be good to go.
"env": {
"node": true,
"jest": true
}
If you are using a .eslintrc or a .eslintrc.json file, please use appropriate syntax.