@@ -1205,7 +1205,11 @@ assert(mocked.find('.greeting').length === 1);
12051205 type : "python" ,
12061206 source,
12071207 } ) ;
1208- return runner ?. runTest ( `assert.equal(runPython('get_five()'), 5)` ) ;
1208+ return runner ?. runTest (
1209+ `({
1210+ test: () => assert.equal(runPython('get_five()'), 5),
1211+ })` ,
1212+ ) ;
12091213 } , source ) ;
12101214
12111215 expect ( result ) . toEqual ( { pass : true } ) ;
@@ -1224,7 +1228,11 @@ assert(mocked.find('.greeting').length === 1);
12241228 const result = await page . evaluate ( async ( ) => {
12251229 const runner = window . FCCTestRunner . getRunner ( "python" ) ;
12261230 await runner ?. init ( { } , 1000 ) ;
1227- return runner ?. runTest ( `assert.equal(runPython('get_five()'), 5)` ) ;
1231+ return runner ?. runTest (
1232+ `({
1233+ test: () => assert.equal(runPython('get_five()'), 5),
1234+ })` ,
1235+ ) ;
12281236 } ) ;
12291237
12301238 expect ( result ) . toMatchObject ( {
@@ -1243,7 +1251,9 @@ assert(mocked.find('.greeting').length === 1);
12431251 type : "python" ,
12441252 } ) ;
12451253 return runner ?. runTest (
1246- `assert.equal(runPython('__name__'), '__main__')` ,
1254+ `({
1255+ test: () => assert.equal(runPython('__name__'), '__main__'),
1256+ })` ,
12471257 ) ;
12481258 } ) ;
12491259
@@ -1274,6 +1284,26 @@ assert(mocked.find('.greeting').length === 1);
12741284 } ) ;
12751285 } ) ;
12761286
1287+ it ( "should reject testStrings that evaluate to an invalid object" , async ( ) => {
1288+ const result = await page . evaluate ( async ( ) => {
1289+ const runner = await window . FCCTestRunner . createTestRunner ( {
1290+ type : "python" ,
1291+ } ) ;
1292+ return runner ?. runTest ( `({ invalid: 'test' })` ) ;
1293+ } ) ;
1294+
1295+ expect ( result ) . toEqual ( {
1296+ err : {
1297+ message :
1298+ "Test string did not evaluate to an object with the 'test' property" ,
1299+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
1300+ stack : expect . stringContaining (
1301+ "Error: Test string did not evaluate to an object with the 'test' property" ,
1302+ ) ,
1303+ } ,
1304+ } ) ;
1305+ } ) ;
1306+
12771307 it ( "should make user code available to the python code as the _code variable" , async ( ) => {
12781308 const result = await page . evaluate ( async ( ) => {
12791309 const runner = await window . FCCTestRunner . createTestRunner ( {
@@ -1283,9 +1313,9 @@ assert(mocked.find('.greeting').length === 1);
12831313 } ,
12841314 } ) ;
12851315
1286- return runner ?. runTest (
1287- ` assert.equal(runPython('_code'), "test = 'value'")` ,
1288- ) ;
1316+ return runner ?. runTest ( `({
1317+ test: () => assert.equal(runPython('_code'), "test = 'value'")
1318+ })` ) ;
12891319 } ) ;
12901320
12911321 expect ( result ) . toEqual ( { pass : true } ) ;
@@ -1296,9 +1326,9 @@ assert(mocked.find('.greeting').length === 1);
12961326 const runner = await window . FCCTestRunner . createTestRunner ( {
12971327 type : "python" ,
12981328 } ) ;
1299- return runner ?. runTest (
1300- ` assert.equal(runPython('_Node("x = 1").get_variable("x")'), 1)` ,
1301- ) ;
1329+ return runner ?. runTest ( `({
1330+ test: () => assert.equal(runPython('_Node("x = 1").get_variable("x")'), 1)
1331+ })` ) ;
13021332 } ) ;
13031333
13041334 expect ( result ) . toEqual ( { pass : true } ) ;
@@ -1316,7 +1346,9 @@ assert(mocked.find('.greeting').length === 1);
13161346 } ,
13171347 } ) ;
13181348
1319- return runner ?. runTest ( "" ) ;
1349+ return runner ?. runTest ( `({
1350+ test: () => {}
1351+ })` ) ;
13201352 } ) ;
13211353
13221354 expect ( result ) . toEqual ( { pass : true } ) ;
@@ -1342,7 +1374,9 @@ assert(mocked.find('.greeting').length === 1);
13421374 const runner = await window . FCCTestRunner . createTestRunner ( {
13431375 type : "python" ,
13441376 } ) ;
1345- return runner ?. runTest ( `assert.equal(runPython('1 + "1"'), 2)` ) ;
1377+ return runner ?. runTest ( `({
1378+ test: () => assert.equal(runPython('1 + "1"'), 2)
1379+ })` ) ;
13461380 } ) ;
13471381 expect ( result ) . toEqual ( {
13481382 err : {
@@ -1368,7 +1402,9 @@ pattern = re.compile('l+')
13681402 type : "python" ,
13691403 source,
13701404 } ) ;
1371- return runner ?. runTest ( `assert.equal(runPython('str(pattern)'), "l+")` ) ;
1405+ return runner ?. runTest ( `({
1406+ test: () => assert.equal(runPython('str(pattern)'), "l+")
1407+ })` ) ;
13721408 } , source ) ;
13731409 expect ( result ) . toEqual ( {
13741410 err : {
@@ -1395,9 +1431,9 @@ pattern = re.compile('l+')`;
13951431 } ) ;
13961432 // Since the comparison includes a PyProxy object, that will be
13971433 // posted back to the caller and cause a DataCloneError
1398- return runner ?. runTest (
1399- ` assert.equal(__userGlobals.get("pattern"), "l+")` ,
1400- ) ;
1434+ return runner ?. runTest ( `
1435+ ({ test: () => assert.equal(__userGlobals.get("pattern"), "l+") })
1436+ ` ) ;
14011437 } , source ) ;
14021438
14031439 expect ( result ) . toEqual ( {
@@ -1415,39 +1451,14 @@ pattern = re.compile('l+')`;
14151451 } ) ;
14161452 } ) ;
14171453
1418- it ( "should support runPython in simple tests" , async ( ) => {
1419- const result = await page . evaluate ( async ( ) => {
1420- const runner = await window . FCCTestRunner . createTestRunner ( {
1421- type : "python" ,
1422- } ) ;
1423- return runner ?. runTest ( `assert.equal(runPython('1 + 1'), 2)` ) ;
1424- } ) ;
1425- expect ( result ) . toEqual ( { pass : true } ) ;
1426- } ) ;
1427-
1428- it ( "should evaluate user code before running tests" , async ( ) => {
1429- const result = await page . evaluate ( async ( ) => {
1430- const runner = await window . FCCTestRunner . createTestRunner ( {
1431- type : "python" ,
1432- source : "x = 3" ,
1433- } ) ;
1434- return runner ?. runTest ( `assert.equal(runPython('x + 1'), 5)` ) ;
1435- } ) ;
1436-
1437- expect ( result ) . toMatchObject ( {
1438- err : {
1439- actual : 4 ,
1440- expected : 5 ,
1441- } ,
1442- } ) ;
1443- } ) ;
1444-
1445- it ( "should not throw when input is called during a test" , async ( ) => {
1454+ it ( "should not throw io exceptions when input is called in a test" , async ( ) => {
14461455 const result = await page . evaluate ( async ( ) => {
14471456 const runner = await window . FCCTestRunner . createTestRunner ( {
14481457 type : "python" ,
14491458 } ) ;
1450- return runner ?. runTest ( `assert.equal(runPython('input("test")'), "")` ) ;
1459+ return runner ?. runTest (
1460+ `({ test: () => assert.equal(runPython('input("test")'), "") })` ,
1461+ ) ;
14511462 } ) ;
14521463
14531464 expect ( result ) . toEqual ( { pass : true } ) ;
@@ -1463,7 +1474,7 @@ pattern = re.compile('l+')`;
14631474 } ,
14641475 } ) ;
14651476 return runner ?. runTest (
1466- `assert.equal(runPython('input()'), "test input")` ,
1477+ `({ test: () => assert.equal(runPython('input()'), "test input") } )` ,
14671478 ) ;
14681479 } , beforeEach ) ;
14691480
@@ -1483,7 +1494,7 @@ pattern = re.compile('l+')`;
14831494 } ,
14841495 } ) ;
14851496 return runner ?. runTest (
1486- `assert.equal(runPython('name'), "mocked input")` ,
1497+ `({ test: () => assert.equal(runPython('name'), "mocked input") } )` ,
14871498 ) ;
14881499 } ,
14891500 source ,
@@ -1492,50 +1503,5 @@ pattern = re.compile('l+')`;
14921503
14931504 expect ( result ) . toEqual ( { pass : true } ) ;
14941505 } ) ;
1495-
1496- it ( "should not automatically fail tests if the source raises an error" , async ( ) => {
1497- const source = `
1498- def func():
1499- pass
1500- ` ;
1501- const result = await page . evaluate ( async ( source ) => {
1502- const runner = await window . FCCTestRunner . createTestRunner ( {
1503- type : "python" ,
1504- source,
1505- } ) ;
1506- return runner ?. runTest ( `assert.equal(1, 2)` ) ;
1507- } , source ) ;
1508-
1509- expect ( result ) . toMatchObject ( {
1510- err : {
1511- expected : 2 ,
1512- actual : 1 ,
1513- } ,
1514- } ) ;
1515- } ) ;
1516-
1517- it ( "should be possible to re-run user code inside a test to detect errors" , async ( ) => {
1518- const source = `
1519- def func():
1520- pass
1521- ` ;
1522-
1523- const result = await page . evaluate ( async ( source ) => {
1524- const runner = await window . FCCTestRunner . createTestRunner ( {
1525- type : "python" ,
1526- source,
1527- code : {
1528- contents : source ,
1529- } ,
1530- } ) ;
1531- return runner ?. runTest ( `runPython(code)` ) ;
1532- } , source ) ;
1533-
1534- expect ( result ) . toMatchObject ( {
1535- err : {
1536- type : "IndentationError" ,
1537- } ,
1538- } ) ;
1539- } ) ;
15401506 } ) ;
15411507} ) ;
0 commit comments