11let { test } = require ( 'uvu' )
2- let { is, ok, equal, match, not } = require ( 'uvu/assert' )
2+ let { is, ok, equal, match, not, throws } = require ( 'uvu/assert' )
33
44let browser = require ( '../index.browser.js' )
5+ let nodeCjs = require ( '../index.cjs' )
56let node = require ( '../index.js' )
67
8+ // `index.js` and `index.cjs` are two entries of the same package.
9+ // They must behave identically, so both are tested here.
10+ let entries = { browser, cjs : nodeCjs , node }
11+
712test . before ( ( ) => {
813 global . crypto = {
914 getRandomValues ( array ) {
@@ -19,9 +24,9 @@ test.after(() => {
1924 delete global . crypto
2025} )
2126
22- for ( let type of [ 'node' , 'browser' ] ) {
27+ for ( let type of [ 'node' , 'cjs' , ' browser'] ) {
2328 let { nanoid, customAlphabet, customRandom, random, urlAlphabet } =
24- type === 'node' ? node : browser
29+ entries [ type ]
2530
2631 test ( `${ type } / nanoid / generates URL-friendly IDs` , ( ) => {
2732 for ( let i = 0 ; i < 100 ; i ++ ) {
@@ -176,7 +181,15 @@ for (let type of ['node', 'browser']) {
176181 }
177182 } )
178183
179- if ( type === 'node' ) {
184+ if ( type !== 'browser' ) {
185+ test ( `${ type } / nanoid / limits ID size` , ( ) => {
186+ is ( nanoid ( 1024 ) . length , 1024 )
187+ throws ( ( ) => nanoid ( 1025 ) , / W r o n g I D s i z e / )
188+ throws ( ( ) => random ( 1025 ) , / W r o n g I D s i z e / )
189+ // A refused ID must not break the pool for the next request.
190+ is ( nanoid ( ) . length , 21 )
191+ } )
192+
180193 test ( `${ type } / proxy number / prevent collision` , ( ) => {
181194 let makeProxyNumberToReproducePreviousID = ( ) => {
182195 let step = 0
0 commit comments