这是jest对path模块中常用功能的测试代码:
import path from 'path'
test("basename", () => {
let x = path.basename('/foo/bar/baz/asdf/quux.html');
expect(x).toEqual('quux.html')
let y = path.basename('/foo/bar/baz/asdf/quux.html', '.html');
expect(y).toEqual('quux')
})
test("delimiter", () => {
let x = path.delimiter
expect(x).toEqual(';')
})
test("dirname", () => {
let x = path.dirname('/foo/bar/baz/asdf/quux');
expect(x).toEqual('/foo/bar/baz/asdf')
})
test("extname", () => {
let x = path.extname('index.html');
expect(x).toEqual('.html')
})
test("format", () => {
let x = path.format({
root: '/ignored',
dir: '/home/user/dir',
base: 'file.txt'
});
expect(x).toEqual('/home/user/dir\\file.txt')
})
test("join", () => {
let x = path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');
expect(x).toEqual("\\foo\\bar\\baz\\asdf")
})
test("normalize", () => {
let x = path.normalize('/foo/bar//baz/asdf/quux/..');
expect(x).toEqual("\\foo\\bar\\baz\\asdf")
})
test("resolve", () => {
let x = path.resolve('/foo/bar', './baz');
expect(x).toEqual("D:\\foo\\bar\\baz")
let y = path.resolve('/foo/bar', '/tmp/file/');
expect(y).toEqual("D:\\tmp\\file")
let z = path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif');
expect(z).toMatch(/D:\\.*\\wwwroot\\static_files\\gif\\image\.gif/)
})
设文件名为path.test.js
,则命令行的执行方法:
npm test -- /path.test.js