Skip to content

Commit

Permalink
switch from should to expect (#1163)
Browse files Browse the repository at this point in the history
* switch from should to expect

* fix build

* install typescript

* fix type test
  • Loading branch information
hipstersmoothie authored Feb 6, 2023
1 parent a2dbeb5 commit 22f2535
Show file tree
Hide file tree
Showing 41 changed files with 1,493 additions and 1,554 deletions.
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"scripts": {
"lint": "eslint --ext .js,.ts,.tsx '**/*.{js,ts}'",
"test": "cross-env BABEL_ENV=test mocha --require @babel/register './packages/**/test/**/*.test.js' --require ts-node/register ./packages/**/test/*.test.ts",
"test": "cross-env BABEL_ENV=test mocha --require @babel/register './packages/**/test/**/*.test.js' ./packages/**/test/*.test.ts",
"test:watch": "npm run test -- --reporter min --watch",
"test:browser": "cross-env BABEL_ENV=test ENV=browser CHROME_BIN=\"$(which chrome || which chromium)\" karma start --single-run",
"test:browser:watch": "npm run -s test:browser start -- --auto-watch",
Expand All @@ -34,7 +34,9 @@
"@babel/cli": "^7.20.7",
"@babel/core": "^7.20.12",
"@babel/preset-env": "^7.20.2",
"@babel/preset-typescript": "^7.18.6",
"@babel/register": "^7.18.9",
"@storybook/expect": "^27.5.2-0",
"@typescript-eslint/eslint-plugin": "^5.50.0",
"@typescript-eslint/parser": "^5.50.0",
"all-contributors-cli": "^6.24.0",
Expand All @@ -49,6 +51,7 @@
"eslint-config-xo": "^0.43.1",
"eslint-plugin-import": "^2.27.5",
"express": "^4.17.1",
"fast-deep-equal": "^3.1.3",
"husky": "^3.0.5",
"karma": "^4.3.0",
"karma-browserify": "^6.1.0",
Expand All @@ -62,8 +65,8 @@
"nyc": "^14.1.1",
"patch-package": "^6.5.1",
"prettier": "^2.8.3",
"should": "^13.2.3",
"source-map-support": "^0.5.21"
"source-map-support": "^0.5.21",
"typescript": "^4.9.5"
},
"husky": {
"hooks": {
Expand Down
3 changes: 0 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
"pixelmatch": "^4.0.2",
"tinycolor2": "^1.4.1"
},
"devDependencies": {
"should": "^13.2.3"
},
"publishConfig": {
"access": "public"
}
Expand Down
28 changes: 14 additions & 14 deletions packages/core/test/utils/mime.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import should from "should/as-function";
import expect from "@storybook/expect";
import { getType, getExtension, addType } from "../../src/utils/mime";

describe("Mime", () => {
Expand All @@ -12,30 +12,30 @@ describe("Mime", () => {

describe("getType", () => {
it("should return undefined if not found", () => {
should(getType("/path/to.the/file.boop")).be.exactly(undefined);
expect(getType("/path/to.the/file.boop")).toBe(undefined);
});

it("should return the correct mime", () => {
should(getType("/path/to.the/file.png")).be.exactly("image/png");
should(getType("/path/to.the/file.gif")).be.exactly("image/gif");
should(getType("/path/to.the/file.jpg")).be.exactly("image/jpeg");
should(getType("/path/to.the/file.jpeg")).be.exactly("image/jpeg");
should(getType("/path/to.the/file.bmp")).be.exactly("image/bmp");
should(getType("/path/to.the/file.tiff")).be.exactly("image/tiff");
expect(getType("/path/to.the/file.png")).toBe("image/png");
expect(getType("/path/to.the/file.gif")).toBe("image/gif");
expect(getType("/path/to.the/file.jpg")).toBe("image/jpeg");
expect(getType("/path/to.the/file.jpeg")).toBe("image/jpeg");
expect(getType("/path/to.the/file.bmp")).toBe("image/bmp");
expect(getType("/path/to.the/file.tiff")).toBe("image/tiff");
});
});

describe("getExtension", () => {
it("should return undefined if not found", () => {
should(getExtension("unknown/mime")).be.exactly(undefined);
expect(getExtension("unknown/mime")).toBe(undefined);
});

it("should return the correct extension", () => {
should(getExtension("image/png")).be.exactly("png");
should(getExtension("image/gif")).be.exactly("gif");
should(getExtension("image/jpeg")).be.exactly("jpeg");
should(getExtension("image/bmp")).be.exactly("bmp");
should(getExtension("image/tiff")).be.exactly("tiff");
expect(getExtension("image/png")).toBe("png");
expect(getExtension("image/gif")).toBe("gif");
expect(getExtension("image/jpeg")).toBe("jpeg");
expect(getExtension("image/bmp")).toBe("bmp");
expect(getExtension("image/tiff")).toBe("tiff");
});
});
});
1 change: 1 addition & 0 deletions packages/custom/types/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"skipLibCheck": true,
"noEmit": true,

// If the library is an external module (uses `export`), this allows your test file to import "mylib" instead of "./index".
Expand Down
10 changes: 5 additions & 5 deletions packages/jimp/test/async.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from "fs";
import should from "should";
import expect from "@storybook/expect";
import { Jimp, getTestDir } from "@jimp/test-utils";

const imagesDir = getTestDir(__dirname) + "/images";
Expand All @@ -16,22 +16,22 @@ describe("Async functions", () => {
const image = await Jimp.read(imagesDir + "/dice.png");
const writtenImage = await image.writeAsync(writePath);

should.exist(writtenImage);
fs.existsSync(writePath).should.be.true();
expect(writtenImage).not.toBeUndefined();
expect(fs.existsSync(writePath)).toBe(true);
fs.unlinkSync(writePath);
});

it("getBuffer returns promise", async () => {
const image = await Jimp.read(imagesDir + "/dice.png");
const buffer = await image.getBufferAsync(Jimp.AUTO);

should.exist(buffer);
expect(buffer.length).toBeGreaterThan(0);
});

it("getBase64 returns promise", async () => {
const image = await Jimp.read(imagesDir + "/dice.png");
const bas64 = await image.getBase64Async(Jimp.AUTO);

should.exist(bas64);
expect(bas64.length).toBeGreaterThan(0);
});
});
4 changes: 3 additions & 1 deletion packages/jimp/test/callbacks.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Jimp, mkJGD, hasOwnProp } from "@jimp/test-utils";
import expect from "@storybook/expect";

import configure from "@jimp/custom";
import plugins from "@jimp/plugins";
import { expectToBeJGD } from "@jimp/test-utils/src";

const jimp = configure({ plugins: [plugins] }, Jimp);

Expand Down Expand Up @@ -213,7 +215,7 @@ describe("Callbacks", () => {

function save(err, image) {
if (err) return done(err);
image.getJGDSync().should.be.sameJGD(result);
expectToBeJGD(image.getJGDSync(), result);
done();
}

Expand Down
83 changes: 43 additions & 40 deletions packages/jimp/test/colordiff.test.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,60 @@
import { Jimp } from "@jimp/test-utils";
import expect from "@storybook/expect";

// Convert [0..1] float to a percent value with only one decimal.
const pct = (n) => ((n * 1000) << 0) / 10;

describe("compute color difference", () => {
it("totally opaque (no alpha defined)", () => {
Jimp.colorDiff(
{ r: 255, g: 0, b: 0 },
{ r: 255, g: 0, b: 0 }
).should.be.equal(0, "both red");

pct(
Jimp.colorDiff({ r: 255, g: 0, b: 0 }, { r: 0, g: 0, b: 0 })
).should.be.equal(33.3, "red x black");

pct(
Jimp.colorDiff({ r: 255, g: 0, b: 0 }, { r: 0, g: 255, b: 0 })
).should.be.equal(66.6, "red x green");

Jimp.colorDiff(
{ r: 255, g: 0, b: 0 },
{ r: 0, g: 255, b: 255 }
).should.be.equal(1, "red x cyan");

Jimp.colorDiff(
{ r: 0, g: 0, b: 0 },
{ r: 255, g: 255, b: 255 }
).should.be.equal(1, "black x white");
expect(
Jimp.colorDiff({ r: 255, g: 0, b: 0 }, { r: 255, g: 0, b: 0 })
).toEqual(0);

expect(
pct(Jimp.colorDiff({ r: 255, g: 0, b: 0 }, { r: 0, g: 0, b: 0 }))
).toEqual(33.3);

expect(
pct(Jimp.colorDiff({ r: 255, g: 0, b: 0 }, { r: 0, g: 255, b: 0 }))
).toEqual(66.6);

expect(
Jimp.colorDiff({ r: 255, g: 0, b: 0 }, { r: 0, g: 255, b: 255 })
).toEqual(1);

expect(
Jimp.colorDiff({ r: 0, g: 0, b: 0 }, { r: 255, g: 255, b: 255 })
).toEqual(1);
});

it("totally transparent", () => {
Jimp.colorDiff(
{ r: 255, g: 0, b: 0, a: 0 },
{ r: 255, g: 0, b: 0, a: 0 }
).should.be.equal(0, "both transparent red");

Jimp.colorDiff(
{ r: 0, g: 0, b: 0, a: 0 },
{ r: 255, g: 255, b: 255, a: 0 }
).should.be.equal(1, "transparent black x transparent white");
expect(
Jimp.colorDiff({ r: 255, g: 0, b: 0, a: 0 }, { r: 255, g: 0, b: 0, a: 0 })
).toEqual(0);

expect(
Jimp.colorDiff(
{ r: 0, g: 0, b: 0, a: 0 },
{ r: 255, g: 255, b: 255, a: 0 }
)
).toEqual(1);
});

it("different alpha", () => {
pct(
Jimp.colorDiff(
{ r: 255, g: 0, b: 0, a: 100 },
{ r: 255, g: 0, b: 0, a: 150 }
expect(
pct(
Jimp.colorDiff(
{ r: 255, g: 0, b: 0, a: 100 },
{ r: 255, g: 0, b: 0, a: 150 }
)
)
).should.be.equal(3.8, "both red");
).toEqual(3.8);

Jimp.colorDiff(
{ r: 0, g: 0, b: 0, a: 0 },
{ r: 255, g: 255, b: 255, a: 255 }
).should.be.equal(1, "transparent black x transparent white");
expect(
Jimp.colorDiff(
{ r: 0, g: 0, b: 0, a: 0 },
{ r: 255, g: 255, b: 255, a: 255 }
)
).toEqual(1);
});
});
6 changes: 3 additions & 3 deletions packages/jimp/test/composite.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from "fs";
import should from "should";
import expect from "@storybook/expect";

import { Jimp, getTestDir } from "@jimp/test-utils";

Expand Down Expand Up @@ -35,7 +35,7 @@ describe("composite", () => {

fs.unlinkSync(testPath);

should.deepEqual(one, two);
expect(one).toEqual(two);
});

it("should handle edges correctly", async () => {
Expand Down Expand Up @@ -64,6 +64,6 @@ describe("composite", () => {

fs.unlinkSync(testPath);

should.deepEqual(one, two);
expect(one).toEqual(two);
});
});
21 changes: 12 additions & 9 deletions packages/jimp/test/diff.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Jimp, mkJGD } from "@jimp/test-utils";
import expect from "@storybook/expect";

describe("Compare image difference", () => {
let imgs = [
Expand All @@ -19,8 +20,9 @@ describe("Compare image difference", () => {

it("images 0 and 1", () => {
const diff = Jimp.diff(imgs[0], imgs[1]);
diff.percent.should.be.equal(0.75);
diff.image.getJGDSync().should.be.sameJGD({

expect(diff.percent).toBe(0.75);
expect(diff.image.getJGDSync()).toEqual({
width: 4,
height: 4,
data: [
Expand All @@ -33,8 +35,9 @@ describe("Compare image difference", () => {

it("images 0 and 2", () => {
const diff = Jimp.diff(imgs[0], imgs[2]);
diff.percent.should.be.equal(0);
diff.image.getJGDSync().should.be.sameJGD({

expect(diff.percent).toBe(0);
expect(diff.image.getJGDSync()).toEqual({
width: 4,
height: 4,
data: [
Expand All @@ -47,8 +50,8 @@ describe("Compare image difference", () => {

it("images 0 and 3", () => {
const diff = Jimp.diff(imgs[0], imgs[3]);
diff.percent.should.be.equal(0);
diff.image.getJGDSync().should.be.sameJGD({
expect(diff.percent).toBe(0);
expect(diff.image.getJGDSync()).toEqual({
width: 4,
height: 4,
data: [
Expand All @@ -60,12 +63,12 @@ describe("Compare image difference", () => {
});

it("allows to set a different threshold", () => {
Jimp.diff(imgs[0], imgs[3], 0.1).percent.should.be.equal(0);
Jimp.diff(imgs[0], imgs[3], 0).percent.should.be.equal(0.25);
expect(Jimp.diff(imgs[0], imgs[3], 0.1).percent).toBe(0);
expect(Jimp.diff(imgs[0], imgs[3], 0).percent).toBe(0.25);
});

it("throws an error if threshold is invalid", () => {
(() => Jimp.diff(imgs[0], imgs[3], "invalid")).should.throw(
expect(() => Jimp.diff(imgs[0], imgs[3], -1)).toThrow(
"threshold must be a number between 0 and 1"
);
});
Expand Down
Loading

0 comments on commit 22f2535

Please sign in to comment.