Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Use Jest as test runner #744

Merged
merged 2 commits into from
Oct 28, 2016
Merged

Use Jest as test runner #744

merged 2 commits into from
Oct 28, 2016

Conversation

thangngoc89
Copy link
Contributor

@thangngoc89 thangngoc89 commented Sep 14, 2016

WARNING: Huge diffs

@thangngoc89
Copy link
Contributor Author

Well, there are 2 integration tests in integration-tests/cli.js that need to be fixed. And I have no idea how to fix them. I'll try to spawn those process instead of execute them

@thangngoc89
Copy link
Contributor Author

Help wanted:

So I have a test file that execute phenomic cli here

That script has require("babel-register") inside it

But when I run the test, it throws error about Syntax Error on spread operator

https://travis-ci.org/MoOx/phenomic/jobs/159970393#L744-L751

I can run that external script manually without a problem

So my question is: Does Jest interfere with babel-register or something ?

To reproduce the error message:

git clone git@github.com:MoOx/phenomic.git
cd phenomic
git checkout jest
npm install
npm run test-phenomic-theme-base

@thangngoc89
Copy link
Contributor Author

@MoOx turn out, the whole problem is because Jest set process.env.NODE_ENV === "test" and we have a proper babel setting for test env.

I see development and production is duplicated here

Can I move them to default babel config?

@MoOx
Copy link
Owner

MoOx commented Sep 15, 2016

No you cannot. Just duplicate again to "test". if you move to global conf, this will make duplicate for webpack-*. I have written a blog post about this mess, will publish this soon ;)

@thangngoc89
Copy link
Contributor Author

Hum. If that is the case, it's really a mess. Babel should have an option that allow us to choose when and when not to extend babel config.

FYI, I'll set NODE_ENV to development than ;)

@MoOx
Copy link
Owner

MoOx commented Sep 15, 2016

You can duplicate, it's just for us, this won't affect docs & boilerplate setup. ANyway it's just a 3 lines section.

@thangngoc89
Copy link
Contributor Author

Yeah. I see. Now that problem was fixed, we have a bigger problem.

Error: listen EADDRINUSE :::8081

Bed time, will look in the in tmr.

@thangngoc89
Copy link
Contributor Author

thangngoc89 commented Sep 15, 2016

All's green (local). Ready for review. Now I can run the hold test suite without freezing my PC.

Copy link
Owner

@MoOx MoOx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made some comment but I will need to do that in multiple passes as it's too huge. I don't like expect api at all and I think we should do this using all api to minimize diff. We are going to miss something and I don't want that :(

@@ -13,7 +13,7 @@ coverage
lib

# tests results
**/__tests__/_output*
**/__tests__/output*
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do the opposite?

@@ -0,0 +1,2 @@
approvals = 1
pattern = "(?i):shipit:|:\\+1:|LGTM"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rebase correctly and remove this file (have been removed yesterday)

@@ -1,11 +1,11 @@
import test from "ava"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you move tests folder? I don't like this name a lot. We might find something better in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is me trying to make jest run test from this folder only. See jestjs/jest#1696

I'll rename it back

"tests": "ava",
"#integration-tests": "needs this order (index test the build, and cli is cleaning it)",
"integration-tests": "ava __tests__/index.js && ava __tests__/cli.js",
"integration-tests": "jest --config '{\"testRegex\": \"integration-tests\", \"setupTestFrameworkScriptFile\": \"<rootDir>/scripts/jest-framework-setup.js\"}'",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan at all of this inline config. We cannot just have a file in this tests folder?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realize that jest accept a file path for configuration. Will fix this.

"coverage": "nyc report --reporter=text-lcov | coveralls",
"all-tests": "cross-env TESTING=1 jest --coverage",
"postall-tests": "npm run test-phenomic-theme-base && npm run docs",
"test": "cross-env TESTING=1 jest",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should rely on NODE_ENV=test instead of TESTING

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is 1 problem with using NODE_ENV=test. The configurator changes NODE_ENV when we use --production flag. And it kills jest

@@ -31,8 +31,6 @@ test("don't break if there is nothing to transform", async (t) => {
})

test("writeAllHTMLFiles", (t) => {
t.plan(3)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You cannot just remove that. We need to be sure all assertions are called.

routesToUrls(routesNoMatches, collection, { log })

expect(log).toBeCalled()
expect(log.mock.calls[0][0]).toContain(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why I don't like expect. This make us too relying on the test framework :'(

Copy link
Contributor Author

@thangngoc89 thangngoc89 Sep 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can pull in expect from npm or chai if you prefer. Jest works great with them. But I am not sure about pretty diff.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to get a similar api as what we have currently. jest-ava-api might be still used and improved :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum... What are you suggesting? AVA doesn't have this built-in. We used sinon for this job.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here for example, we were passing a callback directly. No need to use jest mock since you can pass a log function. I am just not sure how to handle the t.plan pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. We need t.plan or similar to do callback testing like this

@@ -25,7 +25,7 @@ test("should have action to handle get", async (t) => {
})

test("should have action to handle refresh", async (t) => {
t.plan(3)
// t.plan(3)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not an an option. We need to handle that somehow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. That's why I commented out instead of deleted it


import * as module from "../pages"
import reducer from "../pages"

process.env.PHENOMIC_USER_PATHNAME = "/"

test("should have action to handle get", async (t) => {
t.plan(3)
// t.plan(3)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not an an option. We need to handle that somehow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. That's why I commented out instead of deleted it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no reference in Jest's docs for this kind of validation.

}
})
// t.plan(2+5) // 2, err, warn, 5 => array
// t.end()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this check made?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. That's why I commented out instead of deleted it

t.pass()
t.end()
}
describe("bin > commands > setup > inquirer", () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are describe mandatory? What are they for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope. I removed them

routesToUrls(routesNoMatches, collection, { log })

expect(log).toBeCalled()
expect(log.mock.calls[0][0]).toContain(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here for example, we were passing a callback directly. No need to use jest mock since you can pass a log function. I am just not sure how to handle the t.plan pattern.

@thangngoc89
Copy link
Contributor Author

thangngoc89 commented Sep 16, 2016

I gave up on this branch. If you take this over, check out f4b13e4 and starts from there

@MoOx
Copy link
Owner

MoOx commented Oct 24, 2016

I am finishing this.

@MoOx MoOx self-assigned this Oct 24, 2016
@MoOx MoOx force-pushed the jest branch 5 times, most recently from c13d849 to 2d40339 Compare October 26, 2016 20:18
@MoOx MoOx force-pushed the jest branch 5 times, most recently from dfba0de to 1ee2185 Compare October 27, 2016 06:39
@MoOx
Copy link
Owner

MoOx commented Oct 28, 2016

I don't like having to add a babel env test section in base theme but I will find a way to hide all this config behind a preset before cutting a release.

@MoOx MoOx merged commit f8ab913 into master Oct 28, 2016
@MoOx MoOx deleted the jest branch October 28, 2016 19:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants