|
| 1 | +## Switch Chai from `should` style to `expect` <Badge type="tip" text="Has Fix" /> |
| 2 | + |
| 3 | + |
| 4 | +* [Playground Link](/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6InJ1c3QiLCJxdWVyeSI6IiIsInJld3JpdGUiOiIiLCJzdHJpY3RuZXNzIjoicmVsYXhlZCIsInNlbGVjdG9yIjoiIiwiY29uZmlnIjoiaWQ6IHNob3VsZF90b19leHBlY3RfaW5zdGFuY2VvZlxubGFuZ3VhZ2U6IFR5cGVTY3JpcHRcbnJ1bGU6XG4gIGFueTpcbiAgLSBwYXR0ZXJuOiAkTkFNRS5zaG91bGQuYmUuYW4uaW5zdGFuY2VvZigkVFlQRSlcbiAgLSBwYXR0ZXJuOiAkTkFNRS5zaG91bGQuYmUuYW4uaW5zdGFuY2VPZigkVFlQRSlcbmZpeDogfC1cbiAgZXhwZWN0KCROQU1FKS5pbnN0YW5jZU9mKCRUWVBFKVxuLS0tXG5pZDogc2hvdWxkX3RvX2V4cGVjdF9nZW5lcmljU2hvdWxkQmVcbmxhbmd1YWdlOiBUeXBlU2NyaXB0XG5ydWxlOlxuICBwYXR0ZXJuOiAkTkFNRS5zaG91bGQuYmUuJFBST1BcbmZpeDogfC1cbiAgZXhwZWN0KCROQU1FKS50by5iZS4kUFJPUFxuIiwic291cmNlIjoiaXQoJ3Nob3VsZCBwcm9kdWNlIGFuIGluc3RhbmNlIG9mIGNob2tpZGFyLkZTV2F0Y2hlcicsICgpID0+IHtcbiAgd2F0Y2hlci5zaG91bGQuYmUuYW4uaW5zdGFuY2VvZihjaG9raWRhci5GU1dhdGNoZXIpO1xufSk7XG5pdCgnc2hvdWxkIGV4cG9zZSBwdWJsaWMgQVBJIG1ldGhvZHMnLCAoKSA9PiB7XG4gIHdhdGNoZXIub24uc2hvdWxkLmJlLmEoJ2Z1bmN0aW9uJyk7XG4gIHdhdGNoZXIuZW1pdC5zaG91bGQuYmUuYSgnZnVuY3Rpb24nKTtcbiAgd2F0Y2hlci5hZGQuc2hvdWxkLmJlLmEoJ2Z1bmN0aW9uJyk7XG4gIHdhdGNoZXIuY2xvc2Uuc2hvdWxkLmJlLmEoJ2Z1bmN0aW9uJyk7XG4gIHdhdGNoZXIuZ2V0V2F0Y2hlZC5zaG91bGQuYmUuYSgnZnVuY3Rpb24nKTtcbn0pOyJ9) |
| 5 | + |
| 6 | +### Description |
| 7 | + |
| 8 | +[Chai](https://www.chaijs.com) is a BDD / TDD assertion library for JavaScript. It comes with [two styles](https://www.chaijs.com/) of assertions: `should` and `expect`. |
| 9 | + |
| 10 | +The `expect` interface provides a function as a starting point for chaining your language assertions and works with `undefined` and `null` values. |
| 11 | +The `should` style allows for the same chainable assertions as the expect interface, however it extends each object with a should property to start your chain and [does not work](https://www.chaijs.com/guide/styles/#should-extras) with `undefined` and `null` values. |
| 12 | + |
| 13 | +This rule migrates Chai `should` style assertions to `expect` style assertions. Note this is an example rule and a excerpt from [the original rules](https://github.com/43081j/codemods/blob/cddfe101e7f759e4da08b7e2f7bfe892c20f6f48/codemods/chai-should-to-expect.yml). |
| 14 | + |
| 15 | +### YAML |
| 16 | +```yaml |
| 17 | +id: should_to_expect_instanceof |
| 18 | +language: TypeScript |
| 19 | +rule: |
| 20 | + any: |
| 21 | + - pattern: $NAME.should.be.an.instanceof($TYPE) |
| 22 | + - pattern: $NAME.should.be.an.instanceOf($TYPE) |
| 23 | +fix: |- |
| 24 | + expect($NAME).instanceOf($TYPE) |
| 25 | +--- |
| 26 | +id: should_to_expect_genericShouldBe |
| 27 | +language: TypeScript |
| 28 | +rule: |
| 29 | + pattern: $NAME.should.be.$PROP |
| 30 | +fix: |- |
| 31 | + expect($NAME).to.be.$PROP |
| 32 | +``` |
| 33 | +
|
| 34 | +### Example |
| 35 | +
|
| 36 | +<!-- highlight matched code in curly-brace {lineNum} --> |
| 37 | +```js {2,5-9} |
| 38 | +it('should produce an instance of chokidar.FSWatcher', () => { |
| 39 | + watcher.should.be.an.instanceof(chokidar.FSWatcher); |
| 40 | +}); |
| 41 | +it('should expose public API methods', () => { |
| 42 | + watcher.on.should.be.a('function'); |
| 43 | + watcher.emit.should.be.a('function'); |
| 44 | + watcher.add.should.be.a('function'); |
| 45 | + watcher.close.should.be.a('function'); |
| 46 | + watcher.getWatched.should.be.a('function'); |
| 47 | +}); |
| 48 | +``` |
| 49 | + |
| 50 | +### Diff |
| 51 | +<!-- use // [!code --] and // [!code ++] to annotate diff --> |
| 52 | +```js |
| 53 | +it('should produce an instance of chokidar.FSWatcher', () => { |
| 54 | + watcher.should.be.an.instanceof(chokidar.FSWatcher); // [!code --] |
| 55 | + expect(watcher).instanceOf(chokidar.FSWatcher); // [!code ++] |
| 56 | +}); |
| 57 | +it('should expose public API methods', () => { |
| 58 | + watcher.on.should.be.a('function'); // [!code --] |
| 59 | + watcher.emit.should.be.a('function'); // [!code --] |
| 60 | + watcher.add.should.be.a('function'); // [!code --] |
| 61 | + watcher.close.should.be.a('function'); // [!code --] |
| 62 | + watcher.getWatched.should.be.a('function'); // [!code --] |
| 63 | + expect(watcher.on).to.be.a('function'); // [!code ++] |
| 64 | + expect(watcher.emit).to.be.a('function'); // [!code ++] |
| 65 | + expect(watcher.add).to.be.a('function'); // [!code ++] |
| 66 | + expect(watcher.close).to.be.a('function'); // [!code ++] |
| 67 | + expect(watcher.getWatched).to.be.a('function'); // [!code ++] |
| 68 | +}); |
| 69 | +``` |
| 70 | + |
| 71 | +### Contributed by |
| 72 | +[James](https://bsky.app/profile/43081j.com), by [this post](https://bsky.app/profile/43081j.com/post/3lgimzfxza22i) |
| 73 | + |
| 74 | +### Exercise |
| 75 | + |
| 76 | +Exercise left to the reader: can you write a rule to implement [this migration to `node:assert`](https://github.com/paulmillr/chokidar/pull/1409/files)? |
0 commit comments