-
-
Notifications
You must be signed in to change notification settings - Fork 701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add script that registers expect as a side-effect #868
Add script that registers expect as a side-effect #868
Conversation
Hi @DeeperX, thanks for your PR! 😄 This LGTM, but I'd like to hear what the rest of the team thinks about it. |
Hey @DeeperX, that is awesome and fits great with Mocha's approach. Agreed with @lucasfcosta that we should do the same for |
I don't have a strong preference between |
Thanks guys for your comments. I added I believe that |
I like this but I just had another thought: we should document it in the |
@@ -116,6 +116,28 @@ var expect = chai.expect; | |||
var should = chai.should(); | |||
``` | |||
|
|||
## Usage for ES6+ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be named "Native Modules Usage" or something like that. "Usage for ES6" sounds a bit strong and makes it look like its the only way to use it in ES6.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I updated it like you suggested:
This is looking good, except for the title for the ES6 usage. |
Awesome job @DeeperX! |
Cool, this LGTM! Thanks very much for taking the time to address feedback @DeeperX, and kudos for contributing to chai! |
I am sorry for the late response, but I can't find in README something like
Furthermore, import assert from 'chai/assert'
// Using Assert style
import expect from 'chai/expect'
// Using Expect style
import should from 'chai/should' does not look as fancy as import {assert, expect, should} from 'chai' |
@shvaikalesh I can add that warning to the Btw, I don't think that anyone will want to use all of the styles together like this (I guess you just have made it as an example): import { assert, expect, should } from 'chai' But, can use it like this: import { assert } from 'chai' But, why do you fancy on more verbose side rather than the latter simpler one without the braces? I can also do it with braces if everyone is agree. |
@DeeperX Yop, agreed on "all of the styles together" here. Comparing import assert from 'chai/assert' vs import { assert } from 'chai'
|
@shvaikalesh I see your point as valid reasons. I can add an exposing module so we can use the ES6 modules way. |
@DeeperX Thanks for having this discussion & contributing to Chai 👍 . I am curious what other guys think on this matter. |
Yet another point: importing |
@shvaikalesh Your welcome. Thank you for your good ideas. Btw, this pull request is about exposing chai testing styles to CLI and native modules. Do you think that |
@DeeperX Thanks for your continued work on this! There are so many different ways to use Vanilla style: var chai = require('chai');
var assert = chai.assert; // Creates local variable `assert`
var expect = chai.expect; // Creates local variable `expect`
var should = chai.should(); // Creates local variable `should` and modifies `Object.prototype` Vanilla style alternative using ES6 destructuring (really shines when using plugins): const {assert} = require("chai"); // Creates local variable `assert`
const {expect} = require("chai"); // Creates local variable `expect`
const {should} = require("chai"); // Creates local variable `should`
should(); // Modifies `Object.prototype`
const {expect, use} = require("chai"); // Creates local variables `expect` and `use`; useful for plugin use Vanilla style alternative using ES6 destructuring and ES6 modules (really shines when using plugins): import {assert} from "chai"; // Creates local variable `assert`
import {expect} from "chai"; // Creates local variable `expect`
import {should} from "chai"; // Creates local variable `should`
should(); // Modifies `Object.prototype`
import {expect, use} from "chai"; // Creates local variables `expect` and `use`; useful for plugin use Side-effect style: require("chai/assert"); // Creates global variable `assert`
require("chai/expect"); // Creates global variable `expect`
require("chai/should"); // Creates global variable `should` and modifies `Object.prototype` Side-effect style alternative using ES6 modules: import "chai/assert"; // Creates global variable `assert`
import "chai/expect"; // Creates global variable `expect`
import "chai/should"; // Creates global variable `should` and modifies `Object.prototype` Side-effect style alternative using Mocha CLI arguments:
|
I have concerns that it might not be clear to all developers that |
@shvaikalesh Ah yes, laying out all the different styles like this strengthens your earlier argument regarding naming. |
@shvaikalesh For example, Think about this scenario, even we're now exposing it globally, anyone who wants to use it in her code will expose it globally, mostly. import {assert} from "chai"; // exposed it globally
var assert = chai.assert; // exposed it globally Will use assert for the testing in the whole document from a global variable. I don't think that anyone is going to use describe('something', () => {
it('does something', () => {
var assert = chai.assert;
assert.equal(true, true);
})
it('does something lese', () => {
var assert = chai.assert;
assert.equal(false, false);
})
}) It's already has a global usage. I'm +1 for ES6 native module support through an exposed module. But, I'm still undecisive for I'll implement it like that if everyone is agree but will not be happy using it :) |
@meeber Thanks for laying it all out. I think I am not confused anymore 😄. I think that module should either export something or set as global variable/ import { assert } from "chai/register-assert"; // nope
import "chai/register-assert"; // yep |
I endorse this. This is the way it should be documented. With an addition of import { assert } from 'chai'; // yup
import { assert } from 'chai/register-assert'; // nope |
@DeeperX I don't think anyone is suggesting that developers keep redeclaring a variable like |
OK, guys I hear you all. So, to summarize, we will be doing like this, right? Please correct the examples if I missed something: Native Modules Usage (globally)import 'chai/register-assert'
// Using Assert style
import 'chai/register-expect'
// Using Expect style
import 'chai/register-should'
// Using Should style Native Modules Usage (local import only)import { assert } from 'chai';
// Using Assert style
import { expect } from 'chai'
// Using Expect style
import { should } from 'chai'
// Using Should style Usage with Mochamocha spec.js -r chai/register-assert
# Using Assert style
mocha spec.js -r chai/register-expect
# Using Expect style
mocha spec.js -r chai/register-should
# Using Should style |
I don't think a decision has been made yet. I agree with @shvaikalesh that the register scripts should only register a global/modify Object.prototype, not also export. However, I'm still undecided on naming for the side-effect style: import "chai/assert"; // Shorter but less clear that there's a global side-effect
import "chai/register-assert"; // Longer but more clear that there's a global side-effect
In other words, I prefer @shvaikalesh's style when used inside a JavaScript file, but I prefer @DeeperX's style when used as a Mocha require. I think I'm actually leaning toward @DeeperX's style as my preference because I think the side-effect style has a much more common use case in the Mocha context (and we could tailor our documentation toward that end). But both sides make good points. Hmm. Let's get some more opinions: @lucasfcosta @vieiralucas ? |
Hmm, I have mixed feelings about it. |
It looks like the @DeeperX Couple comments regarding the draft you posted above:
|
So, going one turn again. Please check this examples and please let me know if it's OK: Preferably, you should only use only one assertion style in your tests: Pre-Native Modules Usage (registers the chai testing style globally)require('chai/register-assert') // Using Assert style
require('chai/register-expect') // Using Expect style
require('chai/register-should') // Using Should style Pre-Native Modules Usage (as local variables)const { assert } = require("chai") // Using Assert style
const { expect } = require("chai") // Using Expect style
const { should } = require("chai") // Using Should style
should() // Modifies `Object.prototype`
const { expect, use } = require("chai") // Creates local variables `expect` and `use`; useful for plugin use Native Modules Usage (registers the chai testing style globally)import 'chai/register-assert' // Using Assert style
import 'chai/register-expect' // Using Expect style
import 'chai/register-should' // Using Should style Native Modules Usage (local import only)import { assert } from 'chai' // Using Assert style
import { expect } from 'chai' // Using Expect style
import { should } from 'chai' // Using Should style
should() // Modifies `Object.prototype` Usage with Mochamocha spec.js -r chai/register-assert # Using Assert style
mocha spec.js -r chai/register-expect # Using Expect style
mocha spec.js -r chai/register-should # Using Should style |
@DeeperX Again, thank you for your perseverance on this. It's greatly appreciated by the team :D Thoughts:
|
@meeber Thanks for your review, I really appericiate it. No problem btw, let's do it as neat as we can :) I updated my previous post according to your review. And, I'll remove |
Hi, I added new commits to my branch here, but it seems like it didn't appear here. Should I create a new pull request? |
I've added expect and assert as side-effects to make mocha auto register expect and assert from CLI automatically and to use them effectively when importing within native modules.
For more information see: #594 and #604
Native Modules Usage
Usage with Mocha