Webpack SASS loader with alias not compiling under Mocha - sass

I have a component that loads styles like this. The css directory is somewhere else; here it is being used as a Webpack alias.
import 'css/components/PromptText';
// ...
class PromptText extends React.Component {
// ...
}
Here is my webpack.config.json:
var path = require('path');
module.exports = {
entry: './src/index.jsx',
output: {
path: 'dist',
filename: 'app.bundle.js',
},
module: {
loaders: [{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel',
}, {
test: /\.json/,
exclude: /node_modules/,
loader: 'json',
}, {
test: /\.scss/,
exclude: /node_modules/,
loaders: ['style', 'css', 'sass'],
}],
},
resolve: {
alias: {
css: 'css', // <-- Alias here
},
root: path.resolve(__dirname),
extensions: ['', '.js', '.jsx', '.scss'],
},
};
Now, I have a test like this for Mocha:
import PromptText from '../../src/components/PromptText';
describe('PromptText', () => {
it('should display words');
});
When Mocha imports the component, it also tries to load the CSS component. There are two problems with this:
The css directory has been aliased. Mocha needs to know where the alias points. To fix this, I have installed the babel-plugin-webpack-alias package, with the following .babelrc:
{
"presets": ["es2017", "react", "stage-0"],
"env": {
"test": {
"plugins": [
["webpack-alias", {"config": "webpack.config.js"}]
]
}
}
}
CSS can't be imported by Mocha, so it needs to be ignored. I use the ignore-styles package and invoke Mocha as mocha --compilers js:babel-core/register --require ignore-styles.
Despite having done these two things, I get the following error when trying to run tests:
(cd data && make)
make[1]: Nothing to be done for `all'.
NODE_ENV=test ./node_modules/.bin/mocha --compilers js:babel-core/register --require ignore-styles \
$(find test -type f -name 'test*.js')
module.js:341
throw err;
^
Error: Cannot find module 'css'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/waleed/Workspace/js/steno/src/components/PromptText.jsx:2:1)
at Module._compile (module.js:413:34)
at loader (/Users/waleed/Workspace/js/steno/node_modules/babel-register/lib/node.js:146:5)
at Object.require.extensions.(anonymous function) [as .jsx] (/Users/waleed/Workspace/js/steno/node_modules/babel-register/lib/node.js:156:7)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/waleed/Workspace/js/steno/test/components/testPromptText.js:2:1)
at Module._compile (module.js:413:34)
at loader (/Users/waleed/Workspace/js/steno/node_modules/babel-register/lib/node.js:146:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/waleed/Workspace/js/steno/node_modules/babel-register/lib/node.js:156:7)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:20:19)
at /Users/waleed/Workspace/js/steno/node_modules/mocha/lib/mocha.js:220:27
at Array.forEach (native)
at Mocha.loadFiles (/Users/waleed/Workspace/js/steno/node_modules/mocha/lib/mocha.js:217:14)
at Mocha.run (/Users/waleed/Workspace/js/steno/node_modules/mocha/lib/mocha.js:485:10)
at Object.<anonymous> (/Users/waleed/Workspace/js/steno/node_modules/mocha/bin/_mocha:403:18)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:148:18)
at node.js:405:3
make: *** [test] Error 1
For some reason, the css alias isn't being resolved. However, this error only happens sometimes, unpredictably. In order to consistently recreate this error, I have to set BABEL_DISABLE_CACHE=1 in the environment.
How can I get my Mocha test to correctly import and ignore the CSS file?

In order to fix this, I switched from the babel-plugin-webpack-alias to the babel-plugin-webpack-aliases package, and update my .babelrc to use it:
{
"presets": ["es2017", "react", "stage-0"],
"env": {
"test": {
"plugins": [
["webpack-aliases", {"config": "webpack.config.js"}]
]
}
}
}
I also always run mocha with BABEL_DISABLE_CACHE=1 set in the environment. (I think this is undoing the damage that babel-plugin-webpack-alias did; it may not be necessary on every run if that package was never used.)

Related

Laravel Breeze Vite Dev-Server Error: "cannot test case insensitive FS, CLIENT_ENTRY does not point to an existing file"

I set up a fresh Laravel Breeze Project with Vite. When I run:
npm run dev
I get this Error:
failed to load config from PATH/vite.config.js
error when starting dev server:
Error: cannot test case insensitive FS, CLIENT_ENTRY does not point to an existing file: PATH/dist/client/client.mjs
at testCaseInsensitiveFS (PATH/node_modules/vite/dist/node-cjs/publicUtils.cjs:3420:15)
at Object.<anonymous> (PATH/node_modules/vite/dist/node-cjs/publicUtils.cjs:3425:1)
at Module._compile (node:internal/modules/cjs/loader:1126:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
at Object._require.extensions.<computed> [as .js] (file:///PATH/node_modules/vite/dist/node/chunks/dep-6b3a5aff.js:63517:17)
at Module.load (node:internal/modules/cjs/loader:1004:32)
at Function.Module._load (node:internal/modules/cjs/loader:839:12)
at Module.require (node:internal/modules/cjs/loader:1028:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (PATH/node_modules/vite/index.cjs:7:31)
I can't find any Information about this error! A few Weeks ago I had no Problems with this.
Here is my Vite Config File:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
input: [
'resources/css/app.css',
'resources/js/app.js',
],
refresh: true,
}),
],
});
Thanks in advance
Make sure that your project path doesn't contain any special character like #.

Why cypress config is invalid when I try to run npx cypress open?

I've already installed cypress and I once successfully open it and run some example test cases. I don't know why when I try to open it again with command npx cypress open in vscode it says
Cypress Configuration Error
This is the stack trace:
Error: Cannot find module 'cypress'
Require stack:
- C:\Users\janna\Documents\Miftah's folder\Learn\Cypress\gitRepo\ngx-cypress-test\cypress.config.js
- C:\Users\janna\AppData\Local\Cypress\Cache\10.6.0\Cypress\resources\app\node_modules\#packages\server\lib\plugins\child\run_require_async_child.js
- C:\Users\janna\AppData\Local\Cypress\Cache\10.6.0\Cypress\resources\app\node_modules\#packages\server\lib\plugins\child\require_async_child.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (C:\Users\janna\AppData\Local\Cypress\Cache\10.6.0\Cypress\resources\app\node_modules\#cspotcode\source-map-support\source-map-support.js:679:30)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object. (C:\Users\janna\Documents\Miftah's folder\Learn\Cypress\gitRepo\ngx-cypress-test\cypress.config.js:1:26)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at loadFile (C:\Users\janna\AppData\Local\Cypress\Cache\10.6.0\Cypress\resources\app\node_modules\#packages\server\lib\plugins\child\run_require_async_child.js:89:14)
at EventEmitter. (C:\Users\janna\AppData\Local\Cypress\Cache\10.6.0\Cypress\resources\app\node_modules\#packages\server\lib\plugins\child\run_require_async_child.js:116:38)
at EventEmitter.emit (node:events:527:28)
at EventEmitter.emit (node:domain:475:12)
You can fix that file with watchForFileChanges: false,
const { defineConfig } = require("cypress");
module.exports = defineConfig({
e2e: {
watchForFileChanges: false,
setupNodeEvents(on, config) {
// implement node event listeners here
},
},
});

How to compile and bundle react 16.9 using gulp 4.0.1?

I have an existing project, I used React 16.9. Now, I want to implement Gulp 4.0.1 to build it in the server. I am running in windows 10.
In my components, I am using import form
I tried using this gulpfile.js, its very simple.
var gulp = require('gulp'),
browserify = require('browserify'),
source = require('vinyl-source-stream'),
buffer = require('vinyl-buffer');
var BUILD_DIR = 'buildtest/';
function compile() {
var bundler = browserify('src/index.js');
return bundler
.transform('babelify', { presets: ['es2015', 'react'] })
.bundle()
.pipe(source('index.js'))
.pipe(buffer())
.pipe(gulp.dest(BUILD_DIR));
}
gulp.task('build:js', function() {
return compile();
})
gulp.task('build', ['build:js'])
Now by running the gulp command it gives me this error:
λ gulp
assert.js:350
throw err;
^
AssertionError [ERR_ASSERTION]: Task function must be specified
at Gulp.set [as _setTask] (D:\myprojectfolderpath\node_modules\undertaker\lib\set-task.js:10:3)
at Gulp.task (D:\myprojectfolderpath\node_modules\undertaker\lib\task.js:13:8)
at Object.<anonymous> (D:\myprojectfolderpath\gulpfile.js:84:6)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
According to the gulp's task Documentation gulp.task([taskName], taskFunction) second argument the 'taskFunction' needs to use either a gulp.series() or gulp.parallel.
Before 4.0 it was possible to define the task as you did, but this changed in 4.0.
Changing your gulp.task('build', ['build:js']) into gulp.task('build', gulp.series('build:js')) should fix the issue.

Run WebdriverIO tests via Mocha in WebStorm

I want to run WebdriverIO + Mocha tests from the WebStorm gutter (the green triangle icons on the left side, by the line numbers).
But when I press run triangle - error occurred:
/Users/ilyubin/.nvm/versions/node/v10.15.0/bin/node /Users/ilyubin/git/ozon.ru/packages/ozon.ru-ui-tests/node_modules/mocha/bin/_mocha --ui bdd --reporter /Applications/WebStorm.app/Contents/plugins/NodeJS/js/mocha-intellij/lib/mochaIntellijReporter.js /Users/ilyubin/git/ozon.ru/packages/ozon.ru-ui-tests/test/specs/catalog/not.found.page.spec.js --grep "^Тесты страницы ошибок Поиск несуществующего запроса$"
/Users/ilyubin/git/ozon.ru/packages/ozon.ru-ui-tests/test/specs/catalog/not.found.page.spec.js:1
(function (exports, require, module, __filename, __dirname) { import {expect} from 'chai';
^
SyntaxError: Unexpected token {
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at /Users/ilyubin/git/ozon.ru/packages/ozon.ru-ui-tests/node_modules/mocha/lib/mocha.js:324:27
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/Users/ilyubin/git/ozon.ru/packages/ozon.ru-ui-tests/node_modules/mocha/lib/mocha.js:321:14)
at Mocha.run (/Users/ilyubin/git/ozon.ru/packages/ozon.ru-ui-tests/node_modules/mocha/lib/mocha.js:763:10)
at Object.exports.singleRun (/Users/ilyubin/git/ozon.ru/packages/ozon.ru-ui-tests/node_modules/mocha/lib/cli/run-helpers.js:196:16)
at exports.runMocha (/Users/ilyubin/git/ozon.ru/packages/ozon.ru-ui-tests/node_modules/mocha/lib/cli/run-helpers.js:291:13)
at Object.exports.handler.argv [as handler] (/Users/ilyubin/git/ozon.ru/packages/ozon.ru-ui-tests/node_modules/mocha/lib/cli/run.js:292:3)
at Object.runCommand (/Users/ilyubin/git/ozon.ru/packages/ozon.ru-ui-tests/node_modules/mocha/node_modules/yargs/lib/command.js:238:44)
at Object.parseArgs [as _parseArgs] (/Users/ilyubin/git/ozon.ru/packages/ozon.ru-ui-tests/node_modules/mocha/node_modules/yargs/yargs.js:1072:28)
at Object.parse (/Users/ilyubin/git/ozon.ru/packages/ozon.ru-ui-tests/node_modules/mocha/node_modules/yargs/yargs.js:566:25)
at Object.exports.main (/Users/ilyubin/git/ozon.ru/packages/ozon.ru-ui-tests/node_modules/mocha/lib/cli/cli.js:62:6)
at Object.<anonymous> (/Users/ilyubin/git/ozon.ru/packages/ozon.ru-ui-tests/node_modules/mocha/bin/_mocha:10:23)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
How to run WebdriverIO tests from WebStorm?
My versions:
WebStorm 2018.3.3
Build #WS-183.5153.33, built on January 9, 2019
JRE: 1.8.0_152-release-1343-b26 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.2
➜ ~ node -v
v10.15.0
➜ ~ mocha --version
5.2.0
My dependencies:
"dependencies": {
"#wdio/allure-reporter": "^5.4.9",
"#wdio/cli": "^5.4.13",
"#wdio/local-runner": "^5.4.13",
"#wdio/mocha-framework": "^5.4.13",
"#wdio/spec-reporter": "^5.4.3",
"#wdio/sync": "^5.4.13",
...
},
You can't run wdio specs using mocha test runner, so Mocha run configuration can't be used here. You need using Node.js run configuration instead:
Also, as you are using ES6 syntax in your tests, you have to configure wdio to use babel. See https://webdriver.io/docs/babel.html for instructions:
npm install --save-dev #babel/core #babel/cli #babel/preset-env #babel/register
in the project root folder, create a file babel.config.js:
module.exports = {
presets: [
['#babel/preset-env', {
targets: {
node: 8
}
}]
]
}
in your wdio.conf.js, set up mocha as follows;
mochaOpts: {
ui: 'bdd',
timeout: 60000,
compilers: ['js:#babel/register']
},

Angular 2 Protractor cannot require .scss files

I'm trying to run a Protractor Test using Angular 2 and Webpack
Partial Webpack config:
resolve: {
extensions: ['', '.ts', '.js', '.scss', '.css']
},
module: {
loaders: [
// TypeScript
{ test: /\.ts$/, loader: 'ts-loader'},
// SCSS
{ test: /\.scss$/, exclude: /node_modules/, loaders: ['raw-loader', 'sass-loader']}
]
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(true)
]
My home.component.ts
import { Component, Injectable } from '#angular/core';
#Component({
selector: 'home',
styles: [require('./home.component.scss')],
template: `<h4 class="page-home">Home</h4>`
})
#Injectable()
export class Home {}
And my home.component.scss
.page-home {
color: blue;
}
And my E2E:
require('../server');
describe('App', () => {
beforeEach(() => {
browser.get('/');
});
it('should have a title', (done) => {
let subject = browser.getTitle();
subject
.then((data) => {
let result = 'test';
expect(data).toEqual(result);
done();
});
});
it('should definitely pass', () => {
expect(true).toEqual(true);
});
});
I run the server and everything comes up as expected. When I run my E2E tests I get the following error:
[14:42:47] I/hosted - Using the selenium server at http://hub.browserstack.com/wd/hub
[14:42:47] I/launcher - Running 1 instances of WebDriver
[14:43:03] E/launcher - Error: /app/src/app/home/home.component.scss:1
(function (exports, require, module, __filename, __dirname) { .page-home {
^
SyntaxError: Unexpected token .
at Object.exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:513:28)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at home.component.ts:5:12
at Object.<anonymous> (home.component.ts:9:21)
[14:43:03] E/launcher - Process exited with error code 100
npm info lifecycle cnd-app#1.0.0~protractor: Failed to exec protractor script
npm ERR! Linux 3.19.0-59-generic
npm ERR! argv "/usr/local/bin/node" "/app/node_modules/.bin/npm" "run" "protractor"
npm ERR! node v6.2.0
npm ERR! npm v3.9.2
npm ERR! code ELIFECYCLE
npm ERR! cnd-app#1.0.0 protractor: `protractor protractor.conf.js`
npm ERR! Exit status 100
It seems like protractor uses require different than how WebPack treats it.Is there a config for this or anything I am missing?
** Update **
I am able to resolve the issue. I have a typescript register causing conflict in the protractor.conf.js.

Resources