Browse Source

Improve tests

Address infinite loop in test due to invalid fast-check arbitrary declaration.
master
TheoryOfNekomata 3 years ago
parent
commit
ed08901a04
5 changed files with 63 additions and 49 deletions
  1. +1
    -0
      .gitignore
  2. +6
    -1
      package.json
  3. +7
    -0
      src/index.test.ts
  4. +46
    -48
      src/mediumDateTime.test.ts
  5. +3
    -0
      tsconfig.json

+ 1
- 0
.gitignore View File

@@ -3,3 +3,4 @@
node_modules node_modules
dist dist
.idea/ .idea/
coverage/

+ 6
- 1
package.json View File

@@ -1,5 +1,5 @@
{ {
"version": "1.0.0",
"version": "1.0.1",
"license": "MIT", "license": "MIT",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts", "typings": "dist/index.d.ts",
@@ -27,8 +27,13 @@
}, },
"name": "@theoryofnekomata/datetime-commons", "name": "@theoryofnekomata/datetime-commons",
"author": "TheoryOfNekomata <allan.crisostomo@outlook.com>", "author": "TheoryOfNekomata <allan.crisostomo@outlook.com>",
"repository": {
"type": "git",
"url":"https://code.modal.sh/TheoryOfNekomata/datetime-commons.git"
},
"module": "dist/datetime-commons.esm.js", "module": "dist/datetime-commons.esm.js",
"devDependencies": { "devDependencies": {
"date-fns": "^2.16.1",
"fast-check": "^2.3.0", "fast-check": "^2.3.0",
"husky": "^4.3.0", "husky": "^4.3.0",
"tsdx": "^0.13.3", "tsdx": "^0.13.3",


+ 7
- 0
src/index.test.ts View File

@@ -0,0 +1,7 @@
import * as index from './index'

describe('mediumDateTime', () => {
it('should exist', () => {
expect(index.mediumDateTime).toBeDefined()
})
})

+ 46
- 48
src/mediumDateTime.test.ts View File

@@ -5,7 +5,6 @@ import mediumDateTime from './mediumDateTime'
const isInvalidDate = (d: Date) => isNaN(d.getTime()) const isInvalidDate = (d: Date) => isNaN(d.getTime())
const invalidDateString = () => fc.string().filter(s => isInvalidDate(new Date(s))) const invalidDateString = () => fc.string().filter(s => isInvalidDate(new Date(s)))
const validDateString = () => fc.string().filter(s => !isInvalidDate(new Date(s))) const validDateString = () => fc.string().filter(s => !isInvalidDate(new Date(s)))
const invalidDate = () => fc.object().filter(o => !(o as unknown as Date))


it('should exist', () => { it('should exist', () => {
expect(mediumDateTime).toBeDefined() expect(mediumDateTime).toBeDefined()
@@ -20,70 +19,58 @@ it('should accept a minimum of 1 argument', () => {
}) })


describe('on numeric arguments', () => { describe('on numeric arguments', () => {
describe('on invalid values', () => {
it('should throw an error on NaN', () => {
expect(() => mediumDateTime(NaN)).toThrow(RangeError)
})
it('should throw an error on NaN', () => {
expect(() => mediumDateTime(NaN)).toThrow(RangeError)
}) })


describe('on valid values', () => {
it('should return a formatted string', () => {
fc.assert(
fc.property(
fc.integer(),
v => {
expect(typeof mediumDateTime(v)).toBe('string')
}
)
it('should return a formatted string', () => {
fc.assert(
fc.property(
fc.integer(),
v => {
expect(typeof mediumDateTime(v)).toBe('string')
}
) )
})
)
}) })
}) })


describe('on string arguments', () => { describe('on string arguments', () => {
describe('on invalid values', () => {
it('should throw an error', () => {
fc.assert(
fc.property(
invalidDateString(),
v => {
expect(() => mediumDateTime(v)).toThrow(RangeError)
}
)
it('should throw an error given non-parseable values', () => {
fc.assert(
fc.property(
invalidDateString(),
v => {
expect(() => mediumDateTime(v)).toThrow(RangeError)
}
) )
})
)
}) })
describe('on valid values', () => {
it('should return a formatted string', () => {
fc.assert(
fc.property(
validDateString(),
v => {
expect(typeof mediumDateTime(v)).toBe('string')
}
)
it('should return a string given parseable values', () => {
fc.assert(
fc.property(
validDateString(),
v => {
expect(typeof mediumDateTime(v)).toBe('string')
}
) )
})
)
}) })
}) })


describe('on object arguments', () => { describe('on object arguments', () => {
describe('on non-datelike values', () => {
it('should throw an error', () => {
fc.assert(
fc.property(
invalidDate(),
v => {
expect(() => mediumDateTime(v)).toThrow(RangeError)
}
)
it('should throw an error given non-datelike values', () => {
fc.assert(
fc.property(
fc.object(),
v => {
expect(() => mediumDateTime(v)).toThrow(RangeError)
}
) )
})
)
}) })
})


describe('on non-datelike arguments', () => {
it('should throw an error', () => {
it('should throw an error given non-datelike arguments', () => {
fc.assert( fc.assert(
fc.property( fc.property(
fc.anything().filter(v => !['number', 'string', 'object'].includes(typeof v)), fc.anything().filter(v => !['number', 'string', 'object'].includes(typeof v)),
@@ -93,4 +80,15 @@ describe('on non-datelike arguments', () => {
) )
) )
}) })

it('should return a string given date values', () => {
fc.assert(
fc.property(
fc.date(),
d => {
expect(typeof mediumDateTime(d)).toBe('string')
}
)
)
})
}) })

+ 3
- 0
tsconfig.json View File

@@ -11,8 +11,11 @@
"noUnusedLocals": true, "noUnusedLocals": true,
"noUnusedParameters": true, "noUnusedParameters": true,
"noImplicitReturns": true, "noImplicitReturns": true,
"noImplicitAny": true,
"noFallthroughCasesInSwitch": true, "noFallthroughCasesInSwitch": true,
"allowSyntheticDefaultImports": true,
"moduleResolution": "node", "moduleResolution": "node",
"baseUrl": "./",
"jsx": "react", "jsx": "react",
"esModuleInterop": true "esModuleInterop": true
} }


Loading…
Cancel
Save