mochajs / mocha-examples Goto Github PK
View Code? Open in Web Editor NEWWorking examples of common configurations using mocha ☕️
Home Page: https://mochajs.org
License: Apache License 2.0
Working examples of common configurations using mocha ☕️
Home Page: https://mochajs.org
License: Apache License 2.0
I have a file fele.js that specifies all commands that my app needs.
Command Used - Just says hello world when hello is entered.
const program = new commander.Command();
const helloCommand = program.command('hello');
helloCommand
.action(async(options) => {
console.log("Hello World"); //Works well as
return "Hello World";//But this does not work.
})
Now I am using Mocha to test the command. PFB Code
const assert = require('assert');
const { spawn } = require('child_process');
const path = require('path');
describe('Hello Command', function() {
it('should output a greeting message', function(done) {
const expectedOutput = 'Hello World\n';
const cliPath = path.join(__dirname,'fele.js');
const process = spawn('node', [cliPath, 'hello']);
let actual = '';
process.stdout.on('data', data => {
actual += data.toString();
});
process.on('close', () => {
assert.strictEqual(output, expectedOutput);
done();
});
});
});```
Please do suggest me an approach with which I can be able to get not only by using console.log but also by using return statement. Like process.stdout.on works for console.log statements, suggest me some thing that works for a process's method return value.
selenium test is not working in latest chrome
npm install and npm test
my OS : macOS
my Chrome : 77(latest)
SessionNotCreatedError: session not created: Chrome version must be between 71 and 75
(Driver info: chromedriver=2.46.628411 (3324f4c8be9ff2f70a05a30ebc72ffb013e1a71e),platform=Mac OS X 10.14.6 x86_64)
So I edited little of code and Test it
const { Builder, By, Key, Capabilities } = require("selenium-webdriver");
const chrome = require("selenium-webdriver/chrome");
const path = require("chromedriver").path;
const chromeOptions = new chrome.Options().headless();
(async function example() {
var driver = await new Builder(path)
.forBrowser("chrome")
.setChromeOptions(chromeOptions)
.build();
try {
await driver.get("http://www.google.com/ncr");
await driver.findElement(By.name("q")).sendKeys("webdriver", Key.RETURN);
const ret = await driver.findElement(By.css(".LC20lb")).getText();
console.log(ret);
} finally {
await driver.quit();
}
})();
It is worked
edited selenium.spec.js
import "@babel/polyfill";
import chrome from "selenium-webdriver/chrome";
import { Builder, By, Key, Capabilities } from "selenium-webdriver";
import assert from "assert";
import { path } from "chromedriver";
let driver = null;
const chromeOptions = new chrome.Options().headless();
describe("Selenium", () => {
beforeEach(async () => {
driver = await new Builder(path)
.forBrowser("chrome")
.setChromeOptions(chromeOptions)
.build();
await driver.get("http://www.google.com/");
});
afterEach(async () => {
await driver.quit();
});
it('should render "Selenium WebDriver" on a Google search result', async () => {
const element = await driver.findElement(By.name("q"));
await element.sendKeys("webdriver", Key.RETURN);
const res = await driver.findElement(By.css(".LC20lb")).getText();
assert.equal(res, "Selenium WebDriver");
});
});
I added package.json npm install --save-dev chromedriver
and edit like this. But It is not worked occuring same reason.
mochajs/mocha#4182 made me realize there's no example with --reporter=xunit
(https://mochajs.org/#xunit) at the moment. Let's add one in!
Coming over from mochajs/mocha#4981: it would be useful to show modern ways of using Mocha without a pre-test compile script. I.e. showing ts-node
or tsx
or similar as a loader.
Let's add those examples in!
We're going to need an appropriate copyright notice for the Foundation in the README.md
, and an updated LICENSE
file (at minimum).
We will want to hold off on this until we're given the green light to change over the main repo.
mochajs/mocha#4979 prompted me to notice that there isn't an example in this repo of using the tdd
UI. https://mochajs.org/#tdd has a general description but that's it. Let's add one in!
"name": "sd-tracker-api",
"version": "1.0.0",
"description": "",
"main": "main.ts",
"scripts": {
"build": "tsc -p .",
"dev": "SET NODE_ENV=dev & ts-node-dev --respawn --transpile-only -r tsconfig-paths/register src/main.ts",
"test": "SET NODE_ENV=test & mocha --check-leaks -r ts-node/register -r tsconfig-paths/register tests/**.spec.ts"
},
"author": "Jason Terry",
"license": "ISC",
"devDependencies": {
"@types/chai": "^4.3.11",
"@types/express": "^4.17.21",
"@types/mocha": "^10.0.6",
"@types/node": "^20.10.6",
"@types/supertest": "^6.0.2",
"chai": "^5.0.0",
"mocha": "^10.2.0",
"supertest": "^6.3.3",
"ts-node": "^10.9.2",
"ts-node-dev": "^2.0.0",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.3.3"
},
"dependencies": {
"dotenv": "^16.3.1",
"express": "^4.18.2"
}
}
{
"exclude": ["./coverage", "./dist", "./node_modules"],
"ts-node": {
"transpileOnly": true,
"files": true
},
"compilerOptions": {
"target": "es2016",
"module": "Node16",
"rootDir": "./src",
"moduleResolution": "Node16",
"baseUrl": "./src",
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
{
"extension": ["ts"],
"spec": "tests/**/*.spec.ts",
"require": "ts-node/register"
}
Greetings, I am unable to run mocha when including import { expect } from 'chai';
into my project.
It causes the following to occour.
npm run test
> [email protected] test
> SET NODE_ENV=test & mocha --check-leaks -r ts-node/register -r tsconfig-paths/register tests/**.spec.ts
TypeError: Unknown file extension ".ts" for E:\my-learning\sd-tracker-api\tests\types.spec.ts
at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
at defaultLoad (node:internal/modules/esm/load:141:22)
at async ModuleLoader.load (node:internal/modules/esm/loader:409:7)
at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:45)
at async link (node:internal/modules/esm/module_job:76:21)
I am not sure what to do to solve this? I followed the guidance of the following video along with the TS example project.
Any help would be greatly appreciated.
I assume that since older versions of mocha worked with older versions of Angular, that this is still possible, but all of the references I can find online for this are varied and not helpful. I think it would be helpful for this project if there was an official documentation for how to set up mocha for Karma and Angular.
In this example there is a before and after hooks in a separated file using the Mocha --file command.
How can I create variables inside those hooks and to be accessible in other tests? For example:
prepare.js
before(function() {
this.myVariable = 123;
});
myTest.spec.js
describe('Just a simple test', function () {
it('Verifies the myVariable variable', async () => {
expect(this.myVariable).to.eq(123); //should pass
});
});
agh, we need to set up the CLA bot here.
Following #75 -> #76: Chai 5.x is ESM-only and folks are asking about it (mochajs/mocha#5073). We should add an example or two showing how to use Mocha in a CommonJS project with ESM dependencies such as Chai.
Edit: chaijs/chai#1575 has an example of it working, fyi.
TSLint was deprecated years ago: palantir/tslint#4534. Let's move to ESLint!
The best way to check that the code still works.
Summary:
The current workflow runs all tests together in a single job, causing two main issues:
Tracking Difficulty:
Dependency Interference:
Proposed Solutions:
Matrix Strategy:
strategy:
matrix:
folder:
- example1
- example2
- example3
node-version:
- 16
- 18
Separate Repositories:
Could you provide an example of root level hooks like described here for typescript? When I create a hooks.js
or hooks.ts
it doesn't get called.
Is there a formula for using mocha+browser and mocha+node, where tests import JSDOM for node environments only and use global window for browser environments?
Thanks in advance for any advice or help
Leave a comment if you have a request or suggestion for a configuration you think would be useful.
See https://github.com/mochajs/mocha-examples#potential-setups-to-add-in-future for examples.
Moving mochajs/mocha#2866 over here: none of the examples show Mocha working in the browser. Let's fix that by adding one!
Mocha : programmatic-usage, How can we implement babel/other compilations?
Add a typescript sample using ts-node and node's native ESM support.
Note: ts-node uses Node's loader-hooks which are still experimental.
You can find info: ts-node/1007
and a working sample: ts-node/discussion
Add more examples for the express-rest-api
package, preferably handling more methods such as POST
and covering more scenarios for testing (missing fields in req body, headers etc).
=>
https://github.com/mochajs/mocha-examples/tree/master/typescript =
GitHub 404
The link should be:
https://github.com/mochajs/mocha-examples/blob/master/packages/typescript
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.