I'm attempting to set up unit testing in my project, using Jasmine. I am writing my specs in Typescript. My first test is simply checking that a config file returns a value as expected. However, when I import the config, Jasmine can't find the spec. If I take out the import and fill in dummy values, everything works fine.
My spec file is:
/// <reference path="../typings/index.d.ts"/>
process.env.ENV = "test";
process.env.TEST_DB_NAME= "test";
import environment = require("../config/config");
describe("Config Tests:", () => {
it("db returns string", () => {
expect(environment.db).toEqual(process.env.TEST_DB_NAME);
});
});
environment.db should simply return my process.env.TEST_DB_NAME.
I feel this has to do something with the import at the beginning making Jasmine not find the describe(). Anyone know of a way to get Jasmine to work with imports or am I just going about testing this the wrong way?
If you call require directly in your file I think you need to create a module and export it. Another way that I have used import successfully has been to create an interface, export it, and then did something like this.
import IUser = UserList.Interfaces.IUser;
You can then use this as the type for a mock object.
Related
I am using heatmap layer for google maps, and while mapping data for heatmap, i am using constructor new google.maps.LatLng(lat, lng), and everything works file. In index.html file i have loaded //maps.googleapis.com/maps/api... and i guess that is where i get global google object with which i can call that constructor.
So the real issue is when I try to write unit test for that using Jest.
I get the message that google is not defined in global.
I tried to mock global.google in the setup for the tests but couldn't get it done.
use https://www.npmjs.com/package/#googlemaps/jest-mocks.
import { initialize } from "#googlemaps/jest-mocks";
beforeEach(() => {
initialize();
});
// Your tests
how are you ?
I'm having a issue in testing Vuex stuff with Quasar.
Testing components is working normally, but when I started to test my store, I got it.
spec file:
/test/jest/__tests__/store/auth/mutations.spec.js
import { store } from 'src/store';
import { mutations } from 'src/store/auth';
import mutations from 'src/store/auth/mutations';
All those ways I tried to import my store stuff, I got this error:
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { store } from 'quasar/wrappers';
SyntaxError: Cannot use import statement outside a module
how can I import my store in my spec files?
import { store } from 'src/store';
import { mutations } from 'src/store/auth';
import mutations from 'src/store/auth/mutations';
just use require
const store = require('src/store');
const mutations = require('src/store/auth)';
const mutations = require('src/store/auth/mutations');
you need
type=module in package.json to use import
Your problem is that import statements are not supported by plain JavaScript, therefore neither by Node. import is an addition called ES Modules. Since Jest runs your tests in Node, you are getting SyntaxError since import { store } from 'quasar/wrappers' is invalid JS syntax.
The solution is that you need to tell Jest to use Babel (or similar tool) to transform your test file before trying to execute it in Node.
This requires some configuration in your jest.config.js, and also you'd need to install babel-jest, babel and related packages and create a .babelrc or babel.config.js to tell Babel what to do with your test files exactly.
The actual right configuration may depend on many factors, but as a starting point refer to these:
https://jestjs.io/docs/en/getting-started#using-babel
https://jestjs.io/docs/en/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object
https://github.com/facebook/jest/tree/master/packages/babel-jest#setup
Also I'd recommend to consider using Quasar CLI, which gives you a handy way to automatically set up Jest for your Quasar project, including a simple example test you can use as a starting reference: https://testing.quasar.dev/
I’m going through a course for React, and after running jest on my first test, it seems like it should be creating my snapshot, but it doesn’t say it has, and I don't see a snapshot file.
When I change the content in the component and run Jest again, it doesn’t fail like I would expect. I'm just running jest from the command line, and it finds the test, but always passes, regardless of how I change the component. (I assume because it's not creating a snapshot to compare against?)
What might I be doing wrong?
Here is the test:
import React from 'react'
import Search from './Search'
import renderer from 'react-test-renderer'
test('Search snapshot test', () => {
const component = renderer.create(<Search />)
const tree = component.toJSON()
expect(tree).toMatchSnapshot
})
You missed the () at the end:
expect(tree).toMatchSnapshot()
I have created helper file in ember for formatting date using moment.js library. Helper has some if else conditions. When I generated this helper using ember-cli command, a test file also created. But i couldnt write test case inside it. When i run the test, it shows error that wbFormatDate (my helper name) is not a function. Have anybody faced this issue before? Please let me know the best practive to create test cases for helper in ember-cli application
Let's say you have your helper defined in yourAppDir/helpers/wb-format-date.js file. In your test file you should import your helper and perform tests using imported object. Take a look at this:
import { formatDate } from "myApp/helpers/format-date";
module("FormatDateHelper");
test("format-date helper", function(assert) {
var date, result;
date = new Date(Date.UTC(2014, 5, 7));
result = formatDate(date, "YYYY-MM-DD");
return assert.equal(result, "2014-06-07");
});
What is the correct way of placing your helper files and also where should they go with respect to the resolver finding them from an addon ember-cli project?
I am running ember-cli 0.2.2.
I generated an helper from an addon project with:
ember g helper display-helper
The generator placed the file in app/helpers which seemed wrong to me, I would have thought that it should have been placed in addon helpers. I moved the file to addon/helpers and it looks like this:
export default Ember.Handlebars.registerBoundHelper('displayHelper', function displayHelper(searchPath) {
return new Ember.Handlebars.SafeString(get(this, searchPath));
});
When I ran ember test I get the following output:
✘ Error: Assertion Failed: A helper named 'displayHelper' could not be
found
The only way I get this helper to be found by the resolver is to add an import that references the helper in a component that is using it like this:
import displayHelper from '../helpers/display-helper';
This does not seem correct, I would have thought the resolver would have found this automatically?
Also even if I have the reference, the following code ends up with the same error message as above:
import Ember from 'ember';
var get = Ember.get;
function displayHelper(context, searchPath) {
return new Ember.Handlebars.SafeString(get(context, searchPath));
}
export default Ember.Handlebars.makeBoundHelper(displayHelper);
So to sum up, I have to have this line in the component whose template uses the helper:
import displayHelper from '../helpers/display-helper';
And I have to use registerBoundHelper and not makeBoundHelper like the docs say or the helper cannot be found.
If you move your helper from app/helpers to addon/helpers, it is not available in your app namespace. To fix this, add the following file:
// app/helpers/display-helper.js
import displayHelper from 'your-addon-name/helpers/display-helper";
export default displayHelper;
(Do not copy your-addon-name literally, use the name of your addon, which is also your addon's namespace.)
This is based on the instructions here:
http://www.ember-cli.com/#addon-components
Just like the example component there, you can put your real helper code in addons/helpers/display-helper, but you need to import and reexport it to your app for your resolver to find it.