Skip to content

Switch Chai from should style to expect Has Fix

Description

Chai is a BDD / TDD assertion library for JavaScript. It comes with two styles of assertions: should and expect.

The expect interface provides a function as a starting point for chaining your language assertions and works with undefined and null values. 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 with undefined and null values.

This rule migrates Chai should style assertions to expect style assertions. Note this is an example rule and a excerpt from the original rules.

YAML

yaml
id: should_to_expect_instanceof
language: TypeScript
rule:
  any:
  - pattern: $NAME.should.be.an.instanceof($TYPE)
  - pattern: $NAME.should.be.an.instanceOf($TYPE)
fix: |-
  expect($NAME).instanceOf($TYPE)
---
id: should_to_expect_genericShouldBe
language: TypeScript
rule:
  pattern: $NAME.should.be.$PROP
fix: |-
  expect($NAME).to.be.$PROP

Example

js
it('should produce an instance of chokidar.FSWatcher', () => {
  watcher.should.be.an.instanceof(chokidar.FSWatcher);
});
it('should expose public API methods', () => {
  watcher.on.should.be.a('function');
  watcher.emit.should.be.a('function');
  watcher.add.should.be.a('function');
  watcher.close.should.be.a('function');
  watcher.getWatched.should.be.a('function');
});

Diff

js
it('should produce an instance of chokidar.FSWatcher', () => {
  watcher.should.be.an.instanceof(chokidar.FSWatcher); 
  expect(watcher).instanceOf(chokidar.FSWatcher); 
});
it('should expose public API methods', () => {
  watcher.on.should.be.a('function');   
  watcher.emit.should.be.a('function'); 
  watcher.add.should.be.a('function');  
  watcher.close.should.be.a('function'); 
  watcher.getWatched.should.be.a('function'); 
  expect(watcher.on).to.be.a('function'); 
  expect(watcher.emit).to.be.a('function'); 
  expect(watcher.add).to.be.a('function'); 
  expect(watcher.close).to.be.a('function'); 
  expect(watcher.getWatched).to.be.a('function'); 
});

Contributed by

James, by this post

Exercise

Exercise left to the reader: can you write a rule to implement this migration to node:assert?

Made with ❤️ with Rust