Add Jest to TypeScript (and update dependencies)

This commit is contained in:
Anthony Rey 2021-11-09 18:32:42 +01:00
parent 864f34ccc1
commit b0f4a15245
13 changed files with 9585 additions and 127 deletions

12
TypeScript/.editorconfig Normal file
View File

@ -0,0 +1,12 @@
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false

View File

@ -3,8 +3,9 @@
module.exports = { module.exports = {
require: [ require: [
"ts-node/register", "ts-node/register",
"tsconfig-paths/register",
"source-map-support/register" "source-map-support/register"
], ],
recursive: true, recursive: true,
spec: "test/*.spec.ts" spec: "test/mocha/*.spec.ts"
} }

17
TypeScript/.nycrc.js Normal file
View File

@ -0,0 +1,17 @@
module.exports = {
extension: [
".ts"
],
exclude: [
"**/*.d.ts",
"test/**",
"/*.js"
],
require: [
"ts-node/register"
],
reporter: [
"html",
"text"
]
}

33
TypeScript/README.md Normal file
View File

@ -0,0 +1,33 @@
# Gilded Rose
This is the Gilded Rose kata in TypeScript.
## Getting started
Install dependencies
```sh
npm install
```
## Running tests
To run all tests
### Jest way
```sh
npm run test:jest
```
To run all tests in watch mode
```sh
npm run test:jest:watch
```
### Mocha way
```sh
npm run test:mocha
```

View File

@ -1,69 +1,69 @@
export class Item { export class Item {
name: string; name: string;
sellIn: number; sellIn: number;
quality: number; quality: number;
constructor(name, sellIn, quality) { constructor(name, sellIn, quality) {
this.name = name; this.name = name;
this.sellIn = sellIn; this.sellIn = sellIn;
this.quality = quality; this.quality = quality;
} }
} }
export class GildedRose { export class GildedRose {
items: Array<Item>; items: Array<Item>;
constructor(items = [] as Array<Item>) { constructor(items = [] as Array<Item>) {
this.items = items; this.items = items;
} }
updateQuality() { updateQuality() {
for (let i = 0; i < this.items.length; i++) { for (let i = 0; i < this.items.length; i++) {
if (this.items[i].name != 'Aged Brie' && this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') { if (this.items[i].name != 'Aged Brie' && this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') {
if (this.items[i].quality > 0) { if (this.items[i].quality > 0) {
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
this.items[i].quality = this.items[i].quality - 1 this.items[i].quality = this.items[i].quality - 1
} }
}
} else {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1
if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') {
if (this.items[i].sellIn < 11) {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1
}
}
if (this.items[i].sellIn < 6) {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1
}
}
}
}
}
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
this.items[i].sellIn = this.items[i].sellIn - 1;
}
if (this.items[i].sellIn < 0) {
if (this.items[i].name != 'Aged Brie') {
if (this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') {
if (this.items[i].quality > 0) {
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
this.items[i].quality = this.items[i].quality - 1
}
}
} else {
this.items[i].quality = this.items[i].quality - this.items[i].quality
}
} else {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1
}
}
}
} }
} else {
return this.items; if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1
if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') {
if (this.items[i].sellIn < 11) {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1
}
}
if (this.items[i].sellIn < 6) {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1
}
}
}
}
}
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
this.items[i].sellIn = this.items[i].sellIn - 1;
}
if (this.items[i].sellIn < 0) {
if (this.items[i].name != 'Aged Brie') {
if (this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') {
if (this.items[i].quality > 0) {
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
this.items[i].quality = this.items[i].quality - 1
}
}
} else {
this.items[i].quality = this.items[i].quality - this.items[i].quality
}
} else {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1
}
}
}
} }
return this.items;
}
} }

13
TypeScript/jest.config.ts Normal file
View File

@ -0,0 +1,13 @@
import { pathsToModuleNameMapper } from "ts-jest/utils";
const { compilerOptions } = require("./tsconfig");
export default {
roots: ['<rootDir>/app', '<rootDir>/test/jest'],
collectCoverage: true,
coverageDirectory: 'coverage',
coverageProvider: 'v8',
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/' } ),
};

9379
TypeScript/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -6,36 +6,26 @@
"precompile": "rimraf app/**/*.js test/**/*.js", "precompile": "rimraf app/**/*.js test/**/*.js",
"compile": "tsc", "compile": "tsc",
"pretest": "rimraf app/**/*.js test/**/*.js", "pretest": "rimraf app/**/*.js test/**/*.js",
"test": "nyc mocha" "test:jest": "jest",
"test:jest:watch": "jest --watchAll",
"test:mocha": "nyc mocha"
}, },
"license": "MIT", "license": "MIT",
"private": true, "private": true,
"devDependencies": { "devDependencies": {
"@types/chai": "~3.5.2", "@types/chai": "^4.2.22",
"@types/mocha": "~2.2.41", "@types/jest": "^27.0.2",
"@types/node": "~7.0.18", "@types/mocha": "^9.0.0",
"chai": "~4.2.0", "@types/node": "^16.11.7",
"mocha": "~8.2.1", "chai": "^4.3.4",
"jest": "^27.3.1",
"mocha": "^9.1.3",
"nyc": "~15.1.0", "nyc": "~15.1.0",
"rimraf": "~3.0.2", "rimraf": "~3.0.2",
"source-map-support": "0.5.19", "source-map-support": "^0.5.20",
"ts-node": "~9.1.1", "ts-jest": "^27.0.7",
"typescript": "~4.1.3" "ts-node": "^10.4.0",
}, "tsconfig-paths": "^3.11.0",
"nyc": { "typescript": "^4.4.4"
"extension": [
".ts"
],
"exclude": [
"**/*.d.ts",
"test/**"
],
"require": [
"ts-node/register"
],
"reporter": [
"html",
"text"
]
} }
} }

View File

@ -1,12 +0,0 @@
import { expect } from 'chai';
import { Item, GildedRose } from '../app/gilded-rose';
describe('Gilded Rose', function () {
it('should foo', function() {
const gildedRose = new GildedRose([ new Item('foo', 0, 0) ]);
const items = gildedRose.updateQuality();
expect(items[0].name).to.equal('fixme');
});
});

View File

@ -1,27 +1,27 @@
import { Item, GildedRose } from '../app/gilded-rose'; import { Item, GildedRose } from '@/gilded-rose';
const items = [ const items = [
new Item("+5 Dexterity Vest", 10, 20), // new Item("+5 Dexterity Vest", 10, 20), //
new Item("Aged Brie", 2, 0), // new Item("Aged Brie", 2, 0), //
new Item("Elixir of the Mongoose", 5, 7), // new Item("Elixir of the Mongoose", 5, 7), //
new Item("Sulfuras, Hand of Ragnaros", 0, 80), // new Item("Sulfuras, Hand of Ragnaros", 0, 80), //
new Item("Sulfuras, Hand of Ragnaros", -1, 80), new Item("Sulfuras, Hand of Ragnaros", -1, 80),
new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20), new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20),
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49), new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49),
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49), new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49),
// this conjured item does not work properly yet // this conjured item does not work properly yet
new Item("Conjured Mana Cake", 3, 6)]; new Item("Conjured Mana Cake", 3, 6)];
const gildedRose = new GildedRose(items); const gildedRose = new GildedRose(items);
var days: number = 2; var days: number = 2;
for (let i = 0; i < days; i++) { for (let i = 0; i < days; i++) {
console.log("-------- day " + i + " --------"); console.log("-------- day " + i + " --------");
console.log("name, sellIn, quality"); console.log("name, sellIn, quality");
items.forEach(element => { items.forEach(element => {
console.log(element.name + ' ' + element.sellIn + ' ' + element.quality); console.log(element.name + ' ' + element.sellIn + ' ' + element.quality);
}); });
console.log(); console.log();
gildedRose.updateQuality(); gildedRose.updateQuality();
} }

View File

@ -0,0 +1,9 @@
import { Item, GildedRose } from '@/gilded-rose';
describe('Gilded Rose', () => {
it('should foo', () => {
const gildedRose = new GildedRose([new Item('foo', 0, 0)]);
const items = gildedRose.updateQuality();
expect(items[0].name).toBe('fixme');
});
});

View File

@ -0,0 +1,10 @@
import { expect } from 'chai';
import { Item, GildedRose } from '@/gilded-rose';
describe('Gilded Rose', () => {
it('should foo', () => {
const gildedRose = new GildedRose([new Item('foo', 0, 0)]);
const items = gildedRose.updateQuality();
expect(items[0].name).to.equal('fixme');
});
});

View File

@ -1,12 +1,18 @@
{ {
"compilerOptions": { "compilerOptions": {
"module": "commonjs", "module": "commonjs",
"target": "es5", "target": "es5",
"strict": true, "strict": true,
"noImplicitAny": false, "noImplicitAny": false,
"sourceMap": false "sourceMap": false,
}, "baseUrl": "./",
"exclude": [ "paths": {
"node_modules" "@/*": [
] "app/*"
]
}
},
"exclude": [
"node_modules"
]
} }