Absolute Paths with Gulp Mocha and Browserify - unit-testing

I have a project which is using Browserify and ES6 to handle importing and defining packages. The project is using absolute paths using the 'paths' option when building with Gulp-Browserify.
This works fine for the source code, but now I am attempting to write tests with Mocha and run them using gulp-mocha and this is causing problems. Mocha is expecting relative paths, but if I give it a relative path to a file that has other imports using absolute paths, testing will fail with a MODULE_NOT_FOUND
error.
for example
Mocha Import at test/actions/user.js:
import createUser from '../../src/actions/user';
...
Source Import at src/actions/user.js:
import CREATE_USER from 'constants/use
...
will cause a MODULE_NOT_FOUND_ERROR
What I'm wondering is if there is any way to set an absolute path list in mocha similar to how you can for browserify?

You can use app-require-path. Just install it as a dev dep and add the following two files:
test/mocha.opts
--require test/_bootstrap.js
test/_bootstrap.js
require('app-require-path')(__dirname + '/..');
And that's it. You can change the path in _bootstrap.js to whatever you want. You can also add multiple paths. It's up to you.

Related

import static org.assertj.core.api.Assertions.assertThat not suggested by IntelliJ

Newbie, big project, copy/paste unit testing task.
Using IntelliJ Ultimate, when trying to import assertThat(), I get to choose only between:
import static com.mysema.commons.lang.Assert.assertThat;
or
import static org.hamcrest.MatcherAssert.assertThat;
But what i need as an option =>
import static org.assertj.core.api.Assertions.assertThat;
Co-workers have no problem with imports on the same project on their machines.
No error, when i just copy/paste import from another class. But IntelliJ doesn't suggest imports I need!
Any ideas?
Tried to:
invalidate caches
restore default settings
reload all gradle projects
delete .gradle and .idea folders
clean/build project directory
restart pc...
No result
File -> Project Structure -> Global Libraries
Add - New Global Library from Maven -> assertj.core.
Choose version. Apply.

How to import test util files inside of your tests on Dart/Flutter

I have some unit tests and some classes that are related only to tests, like factories for my models, some extensions on mockito and others...
The issue is when I try to import those files from my tests I can just import them with relative paths, lets say I have this utilities:
test/src/test_utils/factories.dart
test/src/test_utils/mocks.dart
test/src/test_utils/mockito_extensions.dart
How I can not do it
But When I try to import them from my tests It can not find them with
import 'package:myapp/test_utils/mocks.dart';
or:
import 'package:myapp/src/test_utils/mocks.dart';
The only one that works
The only way to import them is by relative paths like:
import '../../test_utils/mocks.dart';
The question
I would love to understand what is happening here, why my tests cant find the test utils class on the imports and whats the best way to do this.
In the pub package layout the package: URI format is used for files under lib/. These are the "public libraries" that can be used from outside the package, and so they are the ones associated with the package name. Files under test/ are private to the package and don't need to be referenced by package name - they only can be referenced from neighboring files and directories so a relative import is sufficient. The package: URI format is designed primarily because it allows imports to other packages, to which there is no stable relative path.
See https://dart.dev/tools/pub/package-layout#public-libraries
Using relative imports is the recommended approach.

Where is my test helper for ember-power-select?

I am reading the docs for ember-power-select testings here.
On the setup, it says:
import registerPowerSelectHelpers from '../../tests/helpers/ember-power-select';
registerPowerSelectHelpers();
...
I do not see ember-power-select in my tests/helpers/ directory. Am I supposed to install it separately, or was it supposed to come by default?
I installed ember-power-select by running the command suggested: ember install ember-power-select.
How can I use some of the power-select helpers like selectChoose(), selectSearch(), &c. like prescribed on the docs?
If you look ember-cli explanation about addons; there says "test-support/ - merged with the application’s tests/" and if you look at source code of ember-power-select there is a helper directory under test-support directory. This means when you install ember-power-select; this directory behaves like it is merged with your application's tests directory. It does not matter whether you see the tests/helpers/ember-power-select.js under your project. You can access it like this. Let's assume your project is named sample-project; then you can just import relevant function as follows:
import registerPowerSelectHelpers from 'sample-project/tests/helpers/ember-power-select';
from within your acceptance-test and call it before your test begin registerPowerSelectHelpers(); and you are able to use selectChoose(), selectSearch() as you wish.
Similarly you can just import integration test helpers as follows:
import { typeInSearch, clickTrigger } from 'sample-project/tests/helpers/ember-power-select'

what is difference between chutzpah reference path and typescript reference path

In my work I have seen there is a test project for typescript project(with ts file app1.ts).It is using Chutzpah as test runner.In its config file it has reference path to the the js file generated by ts compiler(app1.js).In test project there is a file appTests.ts in which there is a import statement to import app1.ts.As per my knowledge both are doing same referencing to the same file,But what chutzpah runner is doing with this reference.
The chutzpah_reference is an old way to let Chutzpah know that your file is referencing another one just for testing. You would use this if you knew when building for real deployment you handled this differently. That said, you should not use this anymore and just make use of a Chutzpah.json file.

Unit testing with Webpack, Jasmine (-core), typescript

I have a project that is using webpack to bundle all code into a single file. The project is using Typescript and it is working fine at the moment.
I've gone to add unit testing and jasmine seems to be the way (one of the many ways) forward. Its actually jasmine-core that is included in the package.json - not sure how much of a difference that makes.
So running a very simple test such as
it('true is true', function(){ expect(true).toEqual(true); });
works fine.
But when I add tests that require the use of an import - eg
import MyService = require('./MyServices');
then when I run the tests it complains as it doesn't know what 'require' is.
Uncaught ReferenceError: require is not defined
Now I'm guessing this is because I need to package up the test module in a similar way that I package up the main project.
So what is the best way to do this?
Should I have multiple entry points in the webpack.config.js file - one for each *.spec.ts file?
Or is there a way to have say accept an unknown number of spec files
entry:[ *.spec.ts ] and have it output a js file for each one - *.spec.js
You can use karma/karma-webpack to run all the tests using webpack for resolving the imports. You can take a look at this repository for a simple configuration.
You can also specify an index.spec.ts as en entry point and make this file require all the spec files if you don't want to make one entry point for each spec.ts in your webpack's configuration file.