Moved from jest to mocha

This commit is contained in:
Zechariah 2022-03-28 03:14:11 +08:00
parent b03e38891e
commit 59c6458738
4 changed files with 339 additions and 3388 deletions

View File

@ -11,20 +11,19 @@
"url": "https://github.com/zS1L3NT/ts-npm-ytmusic-api" "url": "https://github.com/zS1L3NT/ts-npm-ytmusic-api"
}, },
"scripts": { "scripts": {
"test": "jest" "test": "ts-mocha --timeout 10000 src/__tests__/**/*.spec.ts"
}, },
"dependencies": { "dependencies": {
"axios": "^0.25.0", "axios": "^0.25.0",
"tough-cookie": "^4.0.0" "tough-cookie": "^4.0.0"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.17.0", "@types/mocha": "^9.1.0",
"@babel/preset-env": "^7.16.11", "@types/node": "^17.0.23",
"@babel/preset-typescript": "^7.16.7",
"@types/jest": "^27.4.0",
"@types/tough-cookie": "^4.0.1", "@types/tough-cookie": "^4.0.1",
"babel-jest": "^27.4.6", "mocha": "^9.2.2",
"jest": "^27.4.7", "mocha.parallel": "^0.15.6",
"ts-mocha": "^9.0.2",
"typescript": "^4.5.5", "typescript": "^4.5.5",
"validate-any": "1.3.1" "validate-any": "1.3.1"
}, },

File diff suppressed because it is too large Load Diff

1
src/@types/mocha.parallel.d.ts vendored Normal file
View File

@ -0,0 +1 @@
declare module "mocha.parallel"

View File

@ -1,3 +1,5 @@
import assert from "assert"
import describeParallel from "mocha.parallel"
import Validator from "validate-any/dist/classes/Validator" import Validator from "validate-any/dist/classes/Validator"
import YTMusic from ".." import YTMusic from ".."
import { import {
@ -16,190 +18,105 @@ import { iValidationError, LIST, STRING, validate } from "validate-any"
const issues: iValidationError[][] = [] const issues: iValidationError[][] = []
const queries = ["Lilac", "Weekend", "Eill", "Eminem", "Lisa Hannigan"] const queries = ["Lilac", "Weekend", "Eill", "Eminem", "Lisa Hannigan"]
const _expect = (data: any, validator: Validator<any>) => { const expect = (data: any, validator: Validator<any>) => {
const { errors } = validate(data, validator) const { errors } = validate(data, validator)
if (errors.length > 0) { if (errors.length > 0) {
issues.push(errors) issues.push(errors)
} }
assert.equal(errors.length, 0)
expect(errors.length).toBe(0)
} }
const ytmusic = new YTMusic() const ytmusic = new YTMusic()
before(() => ytmusic.initialize())
beforeAll(() => ytmusic.initialize())
beforeEach(() => jest.setTimeout(10_000))
queries.forEach(query => { queries.forEach(query => {
describe("Query: " + query, () => { describeParallel("Query: " + query, () => {
test("Search suggestions", done => { it("Search suggestions", async () => {
ytmusic const suggestions = await ytmusic.getSearchSuggestions(query)
.getSearchSuggestions(query) expect(suggestions, LIST(STRING()))
.then(suggestions => {
_expect(suggestions, LIST(STRING()))
done()
})
.catch(done)
}) })
test("Search Songs", done => { it("Search Songs", async () => {
ytmusic const songs = await ytmusic.search(query, "SONG")
.search(query, "SONG") expect(songs, LIST(SONG_DETAILED))
.then(songs => {
_expect(songs, LIST(SONG_DETAILED))
done()
})
.catch(done)
}) })
test("Search Videos", done => { it("Search Videos", async () => {
ytmusic const videos = await ytmusic.search(query, "VIDEO")
.search(query, "VIDEO") expect(videos, LIST(VIDEO_DETAILED))
.then(videos => {
_expect(videos, LIST(VIDEO_DETAILED))
done()
})
.catch(done)
}) })
test("Search Artists", done => { it("Search Artists", async () => {
ytmusic const artists = await ytmusic.search(query, "ARTIST")
.search(query, "ARTIST") expect(artists, LIST(ARTIST_DETAILED))
.then(artists => {
_expect(artists, LIST(ARTIST_DETAILED))
done()
})
.catch(done)
}) })
test("Search Albums", done => { it("Search Albums", async () => {
ytmusic const albums = await ytmusic.search(query, "ALBUM")
.search(query, "ALBUM") expect(albums, LIST(ALBUM_DETAILED))
.then(albums => {
_expect(albums, LIST(ALBUM_DETAILED))
done()
})
.catch(done)
}) })
test("Search Playlists", done => { it("Search Playlists", async () => {
ytmusic const playlists = await ytmusic.search(query, "PLAYLIST")
.search(query, "PLAYLIST") expect(playlists, LIST(PLAYLIST_FULL))
.then(playlists => {
_expect(playlists, LIST(PLAYLIST_FULL))
done()
})
.catch(done)
}) })
test("Search All", done => { it("Search All", async () => {
ytmusic const results = await ytmusic.search(query)
.search(query) expect(
.then(results => {
_expect(
results, results,
LIST( LIST(ALBUM_DETAILED, ARTIST_DETAILED, PLAYLIST_FULL, SONG_DETAILED, VIDEO_DETAILED)
ALBUM_DETAILED,
ARTIST_DETAILED,
PLAYLIST_FULL,
SONG_DETAILED,
VIDEO_DETAILED
) )
)
done()
})
.catch(done)
}) })
test("Get details of the first song result", done => { it("Get details of the first song result", async () => {
ytmusic const songs = await ytmusic.search(query, "SONG")
.search(query, "SONG") const song = await ytmusic.getSong(songs[0]!.videoId)
.then(songs => ytmusic.getSong(songs[0]!.videoId!)) expect(song, SONG_FULL)
.then(song => {
_expect(song, SONG_FULL)
done()
})
.catch(done)
}) })
test("Get details of the first video result", done => { it("Get details of the first video result", async () => {
ytmusic const videos = await ytmusic.search(query, "VIDEO")
.search(query, "VIDEO") const video = await ytmusic.getVideo(videos[0]!.videoId)
.then(videos => ytmusic.getVideo(videos[0]!.videoId!)) expect(video, VIDEO_FULL)
.then(video => {
_expect(video, VIDEO_FULL)
done()
})
.catch(done)
}) })
test("Get details of the first artist result", done => { it("Get details of the first artist result", async () => {
ytmusic const artists = await ytmusic.search(query, "ARTIST")
.search(query, "ARTIST") const artist = await ytmusic.getArtist(artists[0]!.artistId)
.then(artists => ytmusic.getArtist(artists[0]!.artistId!)) expect(artist, ARTIST_FULL)
.then(artist => {
_expect(artist, ARTIST_FULL)
done()
})
.catch(done)
}) })
test("Get the songs of the first artist result", done => { it("Get the songs of the first artist result", async () => {
ytmusic const artists = await ytmusic.search(query, "ARTIST")
.search(query, "ARTIST") const songs = await ytmusic.getArtistSongs(artists[0]!.artistId)
.then(artists => ytmusic.getArtistSongs(artists[0]!.artistId!)) expect(songs, LIST(SONG_DETAILED))
.then(songs => {
_expect(songs, LIST(SONG_DETAILED))
done()
})
.catch(done)
}) })
test("Get the albums of the first artist result", done => { it("Get the albums of the first artist result", async () => {
ytmusic const artists = await ytmusic.search(query, "ARTIST")
.search(query, "ARTIST") const albums = await ytmusic.getArtistAlbums(artists[0]!.artistId)
.then(artists => ytmusic.getArtistAlbums(artists[0]!.artistId!)) expect(albums, LIST(ALBUM_DETAILED))
.then(albums => {
_expect(albums, LIST(ALBUM_DETAILED))
done()
})
.catch(done)
}) })
test("Get details of the first album result", done => { it("Get details of the first album result", async () => {
ytmusic const albums = await ytmusic.search(query, "ALBUM")
.search(query, "ALBUM") const album = await ytmusic.getAlbum(albums[0]!.albumId)
.then(albums => ytmusic.getAlbum(albums[0]!.albumId!)) expect(album, ALBUM_FULL)
.then(album => {
_expect(album, ALBUM_FULL)
done()
})
.catch(done)
}) })
test("Get details of the first playlist result", done => { it("Get details of the first playlist result", async () => {
ytmusic const playlists = await ytmusic.search(query, "PLAYLIST")
.search(query, "PLAYLIST") const playlist = await ytmusic.getPlaylist(playlists[0]!.playlistId)
.then(playlists => ytmusic.getPlaylist(playlists[0]!.playlistId!)) expect(playlist, PLAYLIST_FULL)
.then(playlist => {
_expect(playlist, PLAYLIST_FULL)
done()
})
.catch(done)
}) })
test("Get the videos of the first playlist result", done => { it("Get the videos of the first playlist result", async () => {
ytmusic const playlists = await ytmusic.search(query, "PLAYLIST")
.search(query, "PLAYLIST") const videos = await ytmusic.getPlaylistVideos(playlists[0]!.playlistId)
.then(playlists => ytmusic.getPlaylistVideos(playlists[0]!.playlistId!)) expect(videos, LIST(PLAYLIST_VIDEO))
.then(videos => {
_expect(videos, LIST(PLAYLIST_VIDEO))
done()
})
.catch(done)
}) })
}) })
}) })
afterAll(() => console.log("Issues:", issues)) after(() => console.log("Issues:", issues))