Skip to content

Experimental PR to test switching from .js to .ts #346

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

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ npm-debug.log*
/lib
/coverage
/dist
/ts

.node-version
.vscode
140 changes: 137 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 14 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@
"src",
"lib",
"dist",
"index.d.ts"
"ts"
],
"config": {
"build_file": "dist/testdouble.js"
},
"scripts": {
"clean": "rimraf dist lib coverage",
"clean": "rimraf dist lib coverage ts",
"postclean": "mkdirp dist",
"compile:browser": "cross-conf-env browserify src/index.js --standalone td --outfile $npm_package_config_build_file -p tsify -p headerify",
"compile:node": "tsc",
"compile:browser": "cross-conf-env browserify src/index.ts --standalone td --outfile $npm_package_config_build_file -p tsify -p headerify",
"compile:node": "tsc --project ./tsconfig.node.json",
"precompile": "npm run clean",
"compile": "run-s compile:node compile:browser",
"cover": "nyc --reporter=lcov --reporter=text-summary --reporter=html npm test",
"cover:report": "codeclimate-test-reporter < coverage/lcov.info",
"cover:ci": "if test \"`node -v | awk '{print substr($1, 2, 1)}'`\" = \"8\" ; then run-s cover cover:report ; fi",
"style": "run-p style:js style:ts",
"style:js": "standard --fix",
"style:ts": "standard --fix --parser typescript-eslint-parser --plugin typescript \"**/*.ts\"",
"style:ts": "tslint './src/**/*.ts'",
"test": "teenytest --helper test/helper.js \"test/**/*.test.{js,ts}\"",
"test:all": "run-s test test:example",
"test:unit": "teenytest --helper test/helper.js \"test/unit/**/*.test.{js,ts}\"",
Expand All @@ -51,8 +51,8 @@
"prepare": "npm run compile"
},
"browser": {
"./src/replace/module.js": "./src/replace/module.browser.js",
"quibble": "./src/quibble.browser.js"
"./src/replace/module.ts": "./src/replace/module.browser.ts",
"quibble": "./src/quibble.browser.ts"
},
"standard": {
"globals": [
Expand Down Expand Up @@ -86,10 +86,13 @@
"stringify-object-es5": "^2.5.0"
},
"devDependencies": {
"@types/jest": "^22.2.2",
"@types/lodash": "^4.14.106",
"@types/node": "^9.6.0",
"browserify": "^16.1.0",
"codeclimate-test-reporter": "^0.5.0",
"cross-conf-env": "^1.1.2",
"eslint-plugin-typescript": "^0.9.0",
"eslint-plugin-typescript": "^0.11.0",
"headerify": "^1.0.1",
"is-number": "^5.0.0",
"mkdirp": "^0.5.1",
Expand All @@ -102,6 +105,8 @@
"testdouble": "^3.5.0",
"ts-node": "^5.0.0",
"tsify": "^3.0.4",
"tslint": "^5.9.1",
"tslint-config-standard": "^7.0.0",
"typescript": "^2.7.2",
"typescript-eslint-parser": "^14.0.0"
},
Expand All @@ -111,7 +116,7 @@
"lib": "./lib",
"src": "./src"
},
"typings": "./index.d.ts",
"typings": "./ts/index.d.ts",
"keywords": [
"tdd",
"bdd",
Expand Down
14 changes: 9 additions & 5 deletions src/args-match.js → src/args-match.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import _ from './wrap/lodash'
import isMatcher from './matchers/is-matcher'

export default (expectedArgs, actualArgs, config = {}) => {
export interface Config {
allowMatchers?: boolean
}

export default (expectedArgs, actualArgs, config: Config = {}) => {
if (arityMismatch(expectedArgs, actualArgs, config)) {
return false
} else if (config.allowMatchers !== false) {
Expand All @@ -11,14 +15,14 @@ export default (expectedArgs, actualArgs, config = {}) => {
}
}

var arityMismatch = (expectedArgs, actualArgs, config) =>
const arityMismatch = (expectedArgs, actualArgs, config) =>
expectedArgs.length !== actualArgs.length && !config.ignoreExtraArgs

var equalsWithMatchers = (expectedArgs, actualArgs) =>
const equalsWithMatchers = (expectedArgs, actualArgs) =>
_.every(expectedArgs, (expectedArg, key) =>
argumentMatchesExpectation(expectedArg, actualArgs[key]))

var argumentMatchesExpectation = (expectedArg, actualArg) => {
const argumentMatchesExpectation = (expectedArg, actualArg) => {
if (isMatcher(expectedArg)) {
return matcherTestFor(expectedArg)(actualArg)
} else {
Expand All @@ -30,5 +34,5 @@ var argumentMatchesExpectation = (expectedArg, actualArg) => {
}
}

var matcherTestFor = (matcher) =>
const matcherTestFor = (matcher) =>
matcher.__matches
4 changes: 2 additions & 2 deletions src/callback.js → src/callback.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from './wrap/lodash'
import create from './matchers/create'
import create, { CreatedResult } from './matchers/create'

const callback = create({
name: 'callback',
Expand All @@ -10,7 +10,7 @@ const callback = create({
matcherInstance.args = matcherArgs
matcherInstance.__testdouble_callback = true
}
})
}) as CreatedResult

// Make callback itself quack like a matcher for its non-invoked use case.
callback.__name = 'callback'
Expand Down
21 changes: 16 additions & 5 deletions src/config.js → src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@ import _ from './wrap/lodash'
import log from './log'
import stringifyAnything from './stringify/anything'

const DEFAULTS = {
export interface ConfigObject {
ignoreWarnings?: boolean
promiseConstructor?: PromiseConstructor
suppressErrors?: boolean
}

const DEFAULTS: ConfigObject = {
ignoreWarnings: false,
promiseConstructor: global.Promise,
promiseConstructor: global.Promise as PromiseConstructor,
suppressErrors: false
}
const DELETED_OPTIONS = ['extendWhenReplacingConstructors']

let configData = _.extend({}, DEFAULTS)

export default _.tap((overrides) => {
export interface Config {
(overrides?: ConfigObject): ConfigObject
reset: () => void
}

export default _.tap(((overrides) => {
deleteDeletedOptions(overrides)
ensureOverridesExist(overrides)
return _.extend(configData, overrides)
}, (config) => {
}) as Config, (config) => {
config.reset = () => {
configData = _.extend({}, DEFAULTS)
}
Expand All @@ -30,7 +41,7 @@ const deleteDeletedOptions = (overrides) => {
})
}

var ensureOverridesExist = (overrides) => {
let ensureOverridesExist = (overrides) => {
_.each(overrides, (val, key) => {
if (!configData.hasOwnProperty(key)) {
log.error('td.config',
Expand Down
19 changes: 0 additions & 19 deletions src/constructor.js

This file was deleted.

33 changes: 33 additions & 0 deletions src/constructor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

import { Constructor0, Constructor1, Constructor2, Constructor3, Constructor4 } from './types'

import _ from './wrap/lodash'
import tdFunction from './function'
import imitate from './imitate'

export interface ConstructorType {
<T>(typeOrNames: (keyof T)[] | Constructor0<T>): Constructor0<T>
<A1, T>(typeOrNames: Constructor1<A1, T>): Constructor1<A1, T>
<A1, A2, T>(typeOrNames: Constructor2<A1, A2, T>): Constructor2<A1, A2, T>
<A1, A2, A3, T>(typeOrNames: Constructor3<A1, A2, A3, T>): Constructor3<A1, A2, A3, T>
<A1, A2, A3, A4, T>(typeOrNames: Constructor4<A1, A2, A3, A4, T>): Constructor4<A1, A2, A3, A4, T>
}

const constructor = ((typeOrNames: any) =>
_.isFunction(typeOrNames)
? imitate(typeOrNames)
: fakeConstructorFromNames(typeOrNames)
) as ConstructorType

let fakeConstructorFromNames = <T>(funcNames: (keyof T)[]): Constructor0<T> => {
return _.tap<Constructor0<T>>(tdFunction('(unnamed constructor)') as any, (fakeConstructor) => {
fakeConstructor.prototype.toString = () =>
'[test double instance of constructor]'

_.each(funcNames, (funcName) => {
fakeConstructor.prototype[funcName] = tdFunction(`#${String(funcName)}`)
})
})
}

export default constructor
File renamed without changes.
Loading