I am trying to build unit test for my service implemented in nodeJS.
I understood that I can't use import in my test files so I used only require in my test files:
const typeorm = require("typeorm");
const DeviceService = require("./device.service.ts");
const Device = require("./entities/device.entity.ts");
const CreateDeviceDto = require("./dto/create-device.ts");
The problem is that I have an import in my deviceService.
When I run my test, I have an error from my file device.service.ts:
import { Injectable } from '#nestjs/common';
^
SyntaxError: Unexpected token {
For information, I try in my test to create my service with a stub like this:
const deviceRepository = typeorm.getRepository(Device);
const stub = sinon.stub(deviceRepository, "create").returns(stubValue);
const DeviceServiceStubbed = new DeviceService(deviceRepository);
Stack used: nestJs / Chai / Sinon / Typeorm
Thank you!
The problem is that my mocha call was wrong.
I changed "test": "mocha" to "test": "mocha --require ts-node/register
in my package.json and now it works fine
Related
vue3 project that use firebase notification,
When I moved from MIX to Vite I find this issue
[vite]: Rollup failed to resolve import "firebase/app" from "resources/js/firebase.js".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
error during build:
Error: [vite]: Rollup failed to resolve import "firebase/app" from "resources/js/firebase.js".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
my app.js like the following:
import {messaging, token} from "./firebase";
and my firebase.js:
import { initializeApp } from "firebase/app";
import { getMessaging, getToken } from "firebase/messaging";
const firebaseConfig = {
apiKey: "XXXXXXXXXXXXXXXXXXX_XXXXXXXXXXXXXXXXXXXXX",
authDomain: "XXXXXXXXXX-XXXXXXXXXXX.XXXXXXX.XXX",
projectId: "XXXXXXXXX-XXXXX",
storageBucket: "XXXXXXXXXX.XXXXXXXXXx.XXX",
messagingSenderId: "XXXXXXXXXXXXXXX",
appId: "X:XXXXXXXXXXXXXX:XXX:XXXXXXXXXXXXXXXXXXXXX",
measurementId: "X-XXXXXXXXXXXX"
}
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Initialize Firebase Cloud Messaging and get a reference to the service
export const messaging = getMessaging(app);
export const token = getToken;
export default messaging;
Any help to fix this issue.
Previously it should be set up in cypress.json.
Like
testFiles: [
"e2e/register.cy.ts",
"e2e/buyGiftCertificate.cy.ts",
"e2e/buyMembershipCertificate.cy.ts"
]
But after migrating to Cypress 10 the place for it is cypress.config.ts
There should be a pattern but how to order tests it's not clear
If you are using npx cypress run, you can do exactly the same thing, except use specPattern instead of testFiles.
The following will run the test (only these tests) in the order spec2.cy.js, spec3.cy.js, spec1.cy.js
const { defineConfig } = require("cypress");
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
specPattern: [
"cypress/e2e/spec2.cy.js",
"cypress/e2e/spec3.cy.js",
"cypress/e2e/spec1.cy.js",
]
},
});
Create a file inside your e2e folder as tests-in-order.cy.ts and inside that file import the tests in order you want them to execute:
//Run tests in the intended order
import './register.cy.ts'
import './buyGiftCertificate.cy.ts'
import './buyMembershipCertificate.cy.ts'
And then execute the file using the command (from cli):
npx cypress run --spec=cypress/e2e/tests-in-order.cy.ts
For Test Runner, just click on the file to execute.
I am using NX 14.5.1 and cypress 10.2.0. When I run cypress component testing for "libs/ui" always got "Process not defined" error. In the component call ".env" like this:
import consola from 'consola'
export const logger = consola.create({
level: process.env.NX_ENV_NAME === 'production' ? 0 : 5
})
This is my "cypress.config.ts":
import { defineConfig } from 'cypress';
import { nxComponentTestingPreset } from '#nrwl/react/plugins/component-testing';
export default defineConfig({
component: {
...nxComponentTestingPreset(__dirname)
}
})
And the error is like this:
process is not defined
ReferenceError
The following error originated from your test code, not from Cypress.
> process is not defined
When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.
Cypress could not associate this error to any specific test.
We dynamically generated a new test to display this failure.
I think Cypress doesn't recognize my ".env". How do I pass my ".env" when I run component testing?
I think the basic problem is the app is server-side-rendered. The server compiles in Node where process is available, but the component test runs in the browser where process isn't valid.
This is the way you might tackle it:
In cypress.config.js load the process.env you need into Cypress.env
import { defineConfig } from 'cypress';
import { nxComponentTestingPreset } from '#nrwl/react/plugins/component-testing';
const setupNodeEvents = (on, config) => {
config.env = {
...config.env,
...process.env.NX_ENV_NAME,
}
return config
}
export default defineConfig({
component: {
...nxComponentTestingPreset(__dirname),
setupNodeEvents
}
})
In the component, check where the component is being run from.
import consola from 'consola'
const envName = window && window.Cypress
? window.Cypress.env('NX_ENV_NAME') // running in browser, take Cypress.env
: process.env.NX_ENV_NAME; // running SSR, take process.env
const level = envName === 'production' ? 0 : 5;
export const logger = consola.create({
level
})
This is messy but removes the restriction on running SSR code in a browser environment.
Hi I am trying to add a plugin to implement Mixpanel analytics inside my app.
I am running Nativescript version 6 and added #nstudio/nativescript-mixpanel version 2.1.0.
Getting this error even though my tslint is able to find the module properly.
Cannot find module '#nstudio/nativescript-mixpanel'
Import in main.js
import {
NativeScriptMixpanel,
NativeScriptMixpanelPeople,
} from "#nstudio/nativescript-mixpanel";
const MIXPANEL_TOKEN = "KEY";
NativeScriptMixpanel.init(MIXPANEL_TOKEN);
package.json
"dependencies": {
"#nstudio/nativescript-mixpanel": "^2.1.0",
I have not used Vue NativeScript, however in Vue you would need a slash after the "#"
so
import {
NativeScriptMixpanel,
NativeScriptMixpanelPeople,
} from "#nstudio/nativescript-mixpanel";
const MIXPANEL_TOKEN = "KEY";
NativeScriptMixpanel.init(MIXPANEL_TOKEN);
would become
import {
NativeScriptMixpanel,
NativeScriptMixpanelPeople,
} from "#/nstudio/nativescript-mixpanel";
const MIXPANEL_TOKEN = "KEY";
NativeScriptMixpanel.init(MIXPANEL_TOKEN);
I'm beginning with Svelte and I would like to (more or less) reproduce Mapbox store locator tutorial with Svelte & rollup. (Starting from svelte REPL starter kit).
Everything's fine for loading a map and some markers, but as soon as I try to import this package https://github.com/mapbox/mapbox-gl-geocoder, nothing works anymore and I'm not familiar enough with Svelte to figure out how to setup rollup and fix it.
<script>
import { onMount, setContext } from 'svelte'
import mapbox from 'mapbox-gl/dist/mapbox-gl.js';
import MapboxGeocoder from '#mapbox/mapbox-gl-geocoder'; // <<--- Problem here
mapbox.accessToken = 'xxx';
let map;
let geocoder;
onMount(() => {
map = new mapbox.Map({,,,});
geocoder = new MapboxGeocoder({,,,});
});
</script>
terminal :
bundles src/main.js → public/build/bundle.js...
(!) Missing shims for Node.js built-ins
Creating a browser bundle that depends on 'events'. You might need to include https://github.com/ionic-team/rollup-plugin-node-polyfills
(!) Unresolved dependencies
https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
events (imported by node_modules/#mapbox/mapbox-gl-geocoder/lib/index.js, events?commonjs-external)
(!) Missing global variable name
Use output.globals to specify browser global variable names corresponding to external modules
events (guessing 'events$1')
created public/build/bundle.js in 2s
browser console :
Uncaught ReferenceError: events$1 is not defined
at main.js:5
Then, I tried to add to my rollup config resolve and polyfills plugins, but have other errors.
rollup.config.js
import svelte from 'rollup-plugin-svelte';
import resolve from '#rollup/plugin-node-resolve';
import commonjs from '#rollup/plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import preprocess from 'svelte-preprocess';
import nodeResolve from '#rollup/plugin-node-resolve';
import nodePolyfills from 'rollup-plugin-node-polyfills';
const production = !process.env.ROLLUP_WATCH;
export default {
input: 'src/main.js',
output: {
sourcemap: true,
format: 'iife',
name: 'app',
file: 'public/build/bundle.js'
},
plugins: [
nodeResolve(),
nodePolyfills(),
svelte({
// enable run-time checks when not in production
dev: !production,
// we'll extract any component CSS out into
// a separate file - better for performance
css: css => {
css.write('bundle.css');
},
preprocess: preprocess()
}),
// If you have external dependencies installed from
// npm, you'll most likely need these plugins. In
// some cases you'll need additional configuration -
// consult the documentation for details:
// https://github.com/rollup/plugins/tree/master/packages/commonjs
resolve({
browser: true,
dedupe: ['svelte']
}),
commonjs(),
// In dev mode, call `npm run start` once
// the bundle has been generated
!production && serve(),
// Watch the `public` directory and refresh the
// browser on changes when not in production
!production && livereload('public'),
// If we're building for production (npm run build
// instead of npm run dev), minify
production && terser()
],
watch: {
clearScreen: false
}
};
function serve() {
let started = false;
return {
writeBundle() {
if (!started) {
started = true;
require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
stdio: ['ignore', 'inherit', 'inherit'],
shell: true
});
}
}
};
}
Gives me this
bundles src/main.js → public/build/bundle.js...
LiveReload enabled
(!) `this` has been rewritten to `undefined`
https://rollupjs.org/guide/en/#error-this-is-undefined
node_modules/base-64/base64.js
163: }
164:
165: }(this));
^
to conclude: I'm a bit lost :D
thanks in advance