Why `#import("dart:unittest")` can't run? - unit-testing

I write some dart test code:
#import("dart:unittest");
main() {
test('this is a test', () {
int x = 2+3;
expect(x).equals(5);
});
}
It doesn't display any error in dart editor, but when I press the "run" button, it reports:
Do not know how to load 'dart:unittest''file:///home/freewind/dev/dart/editor
/samples/shuzu.org/test/model_test.dart':
Error: line 1 pos 1: library handler failed
#import("dart:unittest");
^
I see there is a "dart:unittest" library in my dart-sdk. Why it can't be run?

Unfortunately, the unittest library is not yet wired into the dart: namespace. Until that happens, if it ever happens, you'll need to use a relative path to get to the unittest library.
Something like:
#import('path-to-dart/lib/unittest/unitest.dart');
More examples are here: http://api.dartlang.org/unittest.html

This page keeps showing up in Google results for dart and unittest, so I thought I would add an update. The unittest library is now installed quite easily through pub, Dart's package manager. To do this, make sure that you:
check Add pub support when you create a new Dart application.
Then add (or uncomment) the dependency for the unittest package in your pubspec.yaml file. That file should look like this:
name: range
description: A sample application
dependencies:
unittest: { sdk: unittest }
Run pub install (although if you are using Dart Editor, this command should automatically get run for you). Then, in the file where you will be writing your tests,
add this import declaration:
import "package:unittest/unittest.dart";
And you should be good to go.

Related

Looking for documentation setup jest for unit test of admin plugin

my project was created with the swdc create-project ...
Is there a documentation, a tutorial or description for the right setup/configuration unit testing with JEST for custom plugin in administration?
This tutorial describes only how to write a test
But i think there must be a official setup documentation because of versions etc.
EDIT: a tutorial with code is now avialable
Using the suggested solution and execute the test, throws an configuration error:
● Test suite failed to run
Configuration error:
Could not locate module src/core/factory/module.factory mapped as:
undefined/src$1.
Please check your configuration for these entries:
{
"moduleNameMapper": {
"/^src(.*)$/": "undefined/src$1"
},
"resolver": undefined
}
...
Cause of error:
process.env.ADMIN_PATH not setted but required in %Project%/custom/plugins/%MyPlugin%/src/Resources/app/administration/node_modules/#shopware-ag/jest-preset-sw6-admin/jest-preset.js
My solution:
set process.env.ADMIN_PATH in %Project%/custom/plugins/%MyPlugin%/src/Resources/app/administration/jest.config.js
// jest.config.js
...
const { join, resolve } = require('path');
process.env.ADMIN_PATH = resolve('../../../../../../../src/Administration/Resources/app/administration');
...
I think it is easiest to just copy and adapt from a plugin that already has jest tests set up. Look at the administration directory for SwagPayPal for example. Copy the dependency and script sections from their package.json. Also copy the entire jest.config.js. Then within the administration directory of your plugin you should be able to npm install followed by npm run unit or npm run unit-watch and it should find *.spec.js files within the test sub-directory.

How to use a native node.js addon in React

I have a react project in which I would like to use this native node.js addon, which is a wrapper for a C++ SDK.
I've successfully used this module in the past within an Electron project, and can run the sample successfully with node as well.
My question is how I would be able to use this in React, or write my own React friendly solution using the C++ SDK.
I've tried to clone the module and place it under a lib folder in my react project, I ran npm install within that folder to install it's dependencies and tried to run the included example directly with node. This went fine. But using the sample directly from within my React app fails with the following error code ts3client.on is not a function.
So it passes the line were the library is required with var ts3client = require('../../lib/node-ts3sdk-client/api.js'); but that's the furthest I managed to get. I could play around a little bit more, but would like to get some opinions on what might be the best approach here.
Edit
As requested I have added a small example to reproduce the issue I am facing.
Create a simple react app
npm init react-app so-node-addon-react
Clone this repository under src/lib/
git clone https://github.com/svenpaulsen/node-ts3sdk-client.git
Install the module's dependencies
cd node-ts3sdk-client
npm install
Expose some part of the client example by wrapping the try-catch block starting from line 160 in an exported function like so
export function connect() {
try { ... }
catch { ... }
}
Call the function from your project's App.tsx
import { connect } from './lib/node-ts3sdk-client/examples/client_minimal'
...
connect()
Run the project
npm start
This should result in the following error: ts3client.on is not a function

redirecting python import to another module

I'm working on a large open source Python project, which has modules used by both the project and other projects. The goal is to move some of these modules out to a new "library" project that can then be imported by the original project and other projects.
To make this transition smooth, the thought was to copy the modules over to the new project, and have the original project then use the new import. However, to allow other project to have time to migrate later, the thought was to have the original module redirect the import.
For example, the usage is like this in repo 'neutron' (other projects could do the same):
cat neutron/consumer.py
from neutron.redirected import X
print(X)
The in the new 'neutron_lib' project created, the module looks like this (the same as what the original was in project 'neutron'):
cat ../neutron-lib/neutron_lib/redirected.py
X = 5
In the 'neutron' project, I'm trying to do this as the redirect module:
cat neutron/redirected.py
import neutron_lib.redirected
import sys
sys.modules['neutron.redirected'] = neutron_lib.redirected
When I run pylint, it gives these errors:
************* Module neutron.redirected
E: 1, 0: No name 'redirected' in module 'neutron_lib' (no-name-in-module)
************* Module neutron.consumer
E: 1, 0: No name 'X' in module 'neutron.redirected' (no-name-in-module)
If I run this, it runs fine, and consumer.py prints '5'. If I use ipython and load consumer.py, I can see 'X' in dir() output.
Any idea why I'm getting this pylint error? Is it a false error? Is there a way to override it?
Looks like, when running under tox, I can add the following to .pylintrc to hide the errors/warnings
no-name-in-module
nonstandard-exception
When I run pylint it passes now, as does running the Unit tests. Just wish I understood why I'm getting these errors/warnings though.

Unit testing Quartz plugin for Grails

I have some trouble writing the most basic unit test for a job. My problem can be recreated by creating a new job by running grails create-job my in the console.
This will create two files
MyJob.goovy (under the default package myApp - resides in test\unit\myApp)
MyJobSpec.groovy (under the default package myApp - resides in grails-app\jobs\myApp)
Now if i try to use the job MyJob in the test, as example
import myApp.MyJob //This is not resolved
#TestMixin(GrailsUnitTestMixin)
class MyJobSpec extends Specification {
def myJob
I get the compiler error Groovy:unable to resolve class MyJob. Everything so far was automatically created by the plugin. What is going on here? Is there something i did wrong / how do i get this to work?
Using grails 2.3.11.
I don't know which IDE are you using or if you are running everything on a terminal, but I had similar problems with Eclipse and what you need to do is to add grails-app/jobs to the classpath as a source folder.

How to unit test Dart in Phpstorm

I installed the dart plugin, set the SDK home path to the dart-sdk folder, configured the scope and checked the 'Dart SDK enabled' option in Phpstorm 6.0.3. Next, I created a dart file in a 'test' directory with the following code (obviously I added the dependency unittest in my pubspec.yaml and ran pub get):
library mytests;
import 'package:unittest/unittest.dart';
main(){
test('my test', (){
expect(1+1, equals(2));
});
}
When I run this unit test however (by right clicking inside the file and selecting 'Run Test: my test', an error is outputted in the unit testing window stating the following:
C:/dart/dart-sdk/bin/dart.exe --ignore-unrecognized-flags --package-root=C:/Users/myname/dart/pokerdart/test/packages/ C:\Users\myname\AppData\Local\Temp\jetbrains_unit_config.dart
Testing started at 18:55 ...
'file:///C:/Users/myname/AppData/Local/Temp/jetbrains_unit_config.dart': error: line 1 pos 1: unresolved implicit call to super constructor 'Configuration()'
import 'package:unittest/unittest.dart';
^
When I run the test using powershell it just works... Am I missing something/doing something wrong?
Any help is greatly appreciated!
WEB-9636 is fixed in version 7. Please try upgrading to PHPStorm 7.1.3. Or, even better, try PHPStorm 8 EAP (http://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Early+Access+Program) - Dart support has been improved there